I am working in JSON data in a JavaScript object like this:
and I want to delete state data, so it ends in this way:
var data = ["City": "New York"}, {"City": "Fargo"}, {"City": "Los Angels "}];
Currently I am looping through it and removing it but is there no way to remove the object from the city?
I found the "Delete" operator ("Deletes the property off the operator operator object."), But it seems that there is only one item with that property, not as a globally but an example item.
delete object ["state"] // Edit
Edit: My bad I copied / pasted and edited incorrectly. I changed the original polywood, as in the format given in reply to Mr. Polywill Has been used.
EDIT: Finished using the map method suggested by McEnz. The use of the map allows a certain key / value pair (like city) to be drawn from one array to another. You can also drag several types of
newData = data .map (function (v) {return {city: v.City, State: v.State};});
This will work for my purposes and will be better because I am keeping the minority of the key / value pairs. However, it does not appear that there is no solution to work in the original question. For example, if you have 100 different key / price pairs per array item, then you have to add a new array from 99, rather than instead of removing one of the existing ones
You should convert your data into an array of objects and should just work on the array. The example given below uses an array.map to return an array with state properties absent. However, there are many ways of this cat to skin.
(edited to display a filtering prefix before the map)
var data = []; Data.push ({"City": "New York", "State": "New York"}); Data.push ({"City": "Fargo", "State": "North Dakota"}); Data.push ({"City": "Los Angeles", "State": "California"}); Var new data; NewData = Data .filter (function (v) {return v.state! == 'california';}) .map (function (v) {return {city: v.City};}); As the others have indicated, you may not have duplicate properties in your JSN object. Your data is in an array, not in a monster object.
No comments:
Post a Comment