How can I remove a JSON inside a JSON by a key using jQuery?
Iterating through the array of objects and finding the matching object having key as the key value which is captured when clicking on remove button.
var itensArray = [ {"key":1,"idProduct":"Monitor","obsProduct":""}, {"key":2,"idProduct":"Mouse","obsProduct":""}, {"key":3,"idProduct":"Keyboard","obsProduct":""}, {"key":4,"idProduct":"Processor","obsProduct":""} ];//key is captured in a variable on click of remove button.var idProduct = 2;for (var i = 0; i < itensArray.length; i++) { var obj = itensArray[i]; if (obj.key === idProduct) { itensArray.splice(i, 1); }}console.log(itensArray);
JAVASCRIPT: how can I remove a json object in json object?
"extera_Ordering_json[id]"
is simply a string so you need to iterate all the objects in each array and find the one that has that string as name
and check the value
of the same object
Here's a map and filter approach
var data = [ [ {"name":"formtype","value":"ordering"}, {"name":"extera_Ordering_json[id]","value":"1"} ], [ {"name":"extera_Ordering_json[count]","value":"20"}, {"name":"coupon","value":""}, {"name":"formtype","value":"ordering"}, {"name":"extera_Ordering_json[id]","value":"2"} ], [ {"name":"extera_Ordering_json[count]","value":"7"}, {"name":"coupon","value":"1"}, {"name":"formtype","value":"ordering"}, {"name":"extera_Ordering_json[id]","value":"3"} ], [ {"name":"extera_Ordering_json[count]","value":"1"}, {"name":"coupon","value":"1"}, {"name":"formtype","value":"ordering"}, {"name":"extera_Ordering_json[id]","value":"4"} ]]var searchVal = 1data=data.map(function(arr){ return arr.filter(function(o){ return o.name !== "extera_Ordering_json[id]" || +o.value !== searchVal })})
console.log(data)
Remove JSON element
var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.
You can use splice to remove elements from an array.
How to remove JSON object/Token from JSON with changing keys
Try this if it can help you,
var json = File.ReadAllText("json1.json");
var myJobj = JObject.Parse(json);
var rk3 = myJobj.SelectToken("rootkey3");
foreach (var ck in rk3.Children())
{
var ck1 = ck.SelectToken("$..childkey1").ToString();
}
Nested JSON, remove object based on a condition in Jquery using angularjs
You can use _without()
function of _underscorejs
see the documentation
without
_.without(array, values)Returns a copy of the array with all instances of the values removed.
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]
Input
[
{
"market": "Atl",
"subItem": [
{
"comment_id": "1",
"user_id": "32509",
"flag": true
},
{
"comment_id": "2",
"user_id": "32510",
"flag": false
}
]
},
{
"market": "Chicago",
"subItem": [
{
"comment_id": "3",
"user_id": "32501",
"flag": true
},
{
"comment_id": "4",
"user_id": "32502",
"flag": false
}
]
}
]
Output
[
{
"market": "Atl",
"subItem": [
{
"comment_id": "1",
"user_id": "32509",
"flag": true
}
]
},
{
"market": "Chicago",
"subItem": [
{
"comment_id": "3",
"user_id": "32501",
"flag": true
}
]
}
]
Code Snippet
var json = JSON.parse('[{"market":"Atl","subItem":[{"comment_id":"1","user_id":"32509","flag":true},{"comment_id":"2","user_id":"32510","flag":false}]},{"market":"Chicago","subItem":[{"comment_id":"3","user_id":"32501","flag":true},{"comment_id":"4","user_id":"32502","flag":false}]}]');
for(var i=0; i<json.length; i++) { json[i].subItem = _.without(json[i].subItem, _.findWhere(json[i].subItem, {flag: false}));};
console.log(JSON.stringify(json, 0, 8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Delete a element in multi nested JSON using javascript or jQuery
Use JSON.parse
to remove keys that you don't need
function format(json_string, key_to_skip) { return JSON.parse(json_string, function (key, value) { if (key !== key_to_skip) { return value; } }); }
var json = { "CreateprivateNetwork": { "description": "Creatinganetwork.", "input": { "body": { "network": { "name": "$.networkname", "admin_state_up": "sdfdsf" } } }, "action": "neutron.create_network", "publish": {} }};
alert(JSON.stringify(format(JSON.stringify(json), 'name')));
how to remove and edit the values in JSON Array using jquery
Here is a very crude example of transforming your input to desired output.
var input =
[{
id:1,
name:"Test",
ssn:1,
mobile:100,
address:"A"
},
{
id:2,
name:"Test",
ssn:1,
mobile:200,
address:"A"
},
{
id:3,
name:"Temp",
ssn:3,
mobile:300,
address:"C"
},
{
id:4,
name:"Test2",
ssn:4,
mobile:400,
address:"D"
},
{
id:5,
name:"Temp1",
ssn:5,
mobile:500,
address:"E"
}];
//This part transforms your input to a map for each "name" attribute
//Each key has a value of array of "mobile"
var intermediateObject = {};
for(var i = 0; i < input.length; i++) {
if(typeof intermediateObject[input[i].name] == 'undefined') {
intermediateObject[input[i].name] = { ssn: null, address: null, content:[]};
}
intermediateObject[input[i].name].content.push(input[i].mobile);
intermediateObject[input[i].name].ssn = input[i].ssn;
intermediateObject[input[i].name].address= input[i].address;
}
//Here the intermediate transformation is re-adjusted to look like your
//intended output format
var outputObject = [];
var index = 1;
for(elem in intermediateObject ) {
outputObject.push({
id: index++,
name: elem,
ssn: intermediateObject[elem].ssn,
address: intermediateObject[elem].address,
mobile: intermediateObject[elem].content.join(",")
});
}
EDIT: Modified the answer according to edit:
This is the output object:
[{"id":1,"name":"Test","ssn":1,"address":"A","mobile":"100,200"},{"id":2,"name":"Temp","ssn":3,"address":"C","mobile":"300"},{"id":3,"name":"Test2","ssn":4,"address":"D","mobile":"400"},{"id":4,"name":"Temp1","ssn":5,"address":"E","mobile":"500"}]
Related Topics
Drawing Curved Svg Arrow Lines from Div to Div
Javascript Generate Video Thumbnail from Video Url
Formdata Created from an Existing Form Seems Empty When I Log It
Get Previous Value on Input Event
Button Enabled and Disabled Based on Checkbox Is Checked or Not
How to Clear Location.State in React-Router on Page Reload
Jquery Do Not Allow Alphabets to Be Entered in Input Field
Make the Bootstrap Table Scroll-Able Vertically and Horizontally With Fixed Header
How to Pass JavaScript Confirm Variable to PHP Variable
How to Pass Data from Flask to JavaScript in a Template
Html5 and JavaScript to Play Videos Only When Visible
Calculate Percentage JavaScript
How to Save an Image to Localstorage and Display It on the Next Page
Destroy Chart.Js Bar Graph to Redraw Other Graph in Same <Canvas>
What Is the Purpose of the Dollar Sign in JavaScript