简体   繁体   中英

Trying to remove keys from array of objects

I've got a Javascript array with a set of objects and I'm trying to remove the key for each of the objects within that array (ie the 0s and 1s), however I'm struggling to find a Javascript function that does the trick.

 [{ '0': { id: '630640', stuff: 'name1', anotherProp: 'prop1' }, '1': { id: '630640', stuff: 'name2', anotherProp: 'prop2' }, id: '630640' }, { '0': { id: '694969', stuff: 'name3', anotherProp: 'prop3' }, id: '694969' }, undefined ] 

I've tried the following but it doesn't remove the keys.

 var keys = Object.keys(input), output = []; for (var i = 0, length = keys.length; i < length; ++i) output.push(input[keys[i]]); console.log(output); 

You can first filter out all non-null values from arrays then use Object.values to get the values ob each object

 let input = [{ '0': { id: '630640', stuff: 'name1', anotherProp: 'prop1' }, '1': { id: '630640', stuff: 'name2', anotherProp: 'prop2' }, id: '630640' }, { '0': { id: '694969', stuff: 'name3', anotherProp: 'prop3' }, id: '694969' }, undefined ]; let output = input.filter(nonNull => nonNull).map(obj => Object.values(obj)); console.log(output) 

You could use Object.values to just get all the values of an object . If you also want to support older browsers you could instead use Object.keys and map the values yourself.

 var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`); console.log(input.filter(Boolean).map(function(item) { return Object.values(item); })); 

You could also try this format: {630640: [{},{}], 634969: [{}]}

 var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`); console.log(input.filter(Boolean).reduce(function(result, item) { result[item.id] = Object.values(item).filter(function(property) { return typeof property === "object" && property; }); return result; }, {})); 

Or this format: [{id:630640, list: [{},{}]},{id:634969, list: [{}]}]

 var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`); console.log(input.filter(Boolean).map(function(item) { return { id: item.id, list: Object.values(item).filter(function(property) { return typeof property === "object" && property; }) }; })); 

If you want to support older browser support you can polyfill Object.values like this:

 Object.defineProperty(Object, "values", { configurable: false, writable: false, value: function(obj) { return Object.keys(obj).map(function(key) { return obj[key]; }); } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM