in Education by
Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": "Furniture & Fixture", "choice": { "0": { "req_goods": "table", "qty": "10" }, "1": { "req_goods": "chair", "qty": "5" } } }, { "id": 2, "name": "Miscellaneous Property", "choice": { "0": { "req_goods": "Office Rent", "qty": "1" } } }] here how do I remove choice 1 of id 1 . HTML
JS $scope.capital_budgets = [{"id":1,"name":"Furniture & Fixture"}, {"id":2,"name":"Miscellaneous Property"}]; $scope.choices = [{}]; $scope.choices[0] = [{}]; $scope.choices[1] = [{}]; $scope.choices[2] = [{}]; $scope.choices[3] = [{}]; $scope.choices[4] = [{}]; $scope.addNewChoice = function(id) { $scope.choices[id].push({}); }; $scope.removeChoice = function(parent_id, id) { $scope.choices[parent_id].splice(id, 1); }; The Above removeChoice() remove last element but I want to remove the element that user choose to remove. please help i have been trying from 2 days. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can make 'choice' of the array type as follows and use the index of the particular choice in the ng-repeat directive to remove the choice from the choices array. angular .module('demo', []) .controller('DefaultController', DefaultController); function DefaultController() { var vm = this; vm.items = [ { "id": 1, "name": "Furniture & Fixture", "choices": [ { "id": 1, "req_goods": "table", "qty": "10" }, { "id": 2, "req_goods": "chair", "qty": "5" }] }, { "id": 2, "name": "Miscellaneous Property", "choices": [ { "id": 1, "req_goods": "Office Rent", "qty": "1" }] }]; vm.removeChoice = removeChoice; vm.addChoice = addChoice; function removeChoice(itemId, index) { for (var i = 0; i < vm.items.length; i++) { if (vm.items[i].id === itemId) { vm.items[i].choices.splice(index, 1); break; } } } function addChoice(index) { var id = vm.items[index].choices.length + 1; vm.items[index].choices.push({ id: id, req_goods: "", qty: 0 }); } }

{{item.name}}

Run code snippetExpand snippet

Related questions

0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    i have an array of objects in [ { "country": "USA", "payment": [ { "paymentType": "Visa" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I am using Angularjs, I have two api in method in factory method. I tried to access that two ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    How can set row number for each table row created by dir-paginate angularJs. I use two way code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am trying to create an app using AngularJS that takes a user's location (from their mobile) and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    What are the differences between AngularJS and Angular?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    AngularJS Vs. Angular 2 Vs. Angular 4: Understanding the Differences...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    I'm working on a site that has a dynamically generated FAQ and I'm trying to get nested accordions ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I am building a basic RSS reader app that, which the 'story' is clicked, the information for it is displayed in a modal ... .log('Does Not Work!'); Or another way: - overscroll...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    Below is a crude for-loop to illustrate what I need to do. Basically, if there are any 'Variable' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...