简体   繁体   English

尝试从对象数组中删除键

[英]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. 我有一个带有一组对象的Javascript数组,我试图删除该数组中每个对象的键(即0和1),但是我正在努力寻找一个执行以下操作的Javascript函数:特技。

 [{ '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 您可以先从数组中过滤掉所有非空值,然后使用Object.values获取每个对象的值

 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 . 您可以使用Object.values来获取object所有值。 If you also want to support older browsers you could instead use Object.keys and map the values yourself. 如果您还希望支持较旧的浏览器,则可以使用Object.keys并自己映射值。

 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: [{}]} 您也可以尝试使用以下格式: {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: [{}]}] 或以下格式: [{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.values

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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