简体   繁体   English

从嵌套数组到对象数组

[英]From nested array to array of object

I have a nested array like this 我有这样的嵌套数组

array = [[1, 698],[32, 798],[69, 830],[95, 500]]

I want to have a function that return the result in this format 我想要一个以这种格式返回结果的函数

[
    {
        id: 1,
        score: 698
    },
    {
        id: 32,
        score: 798
    },
    {
        id: 69,
        score:830
    },
  ..... the rest of the array
]

I did use a for loop but with no success, and I have no idea on how to aproach the situation. 我确实使用了for循环,但没有成功,我不知道如何处理这种情况。

for(var i = 0; i <= array.lenght ; i++){
    var obj={}
    var res = []
    res.push(array[i])
}

You can take the advantage of the power of the ES6 syntax: 您可以利用ES6语法的强大功能:

 var array = [ [1, 698], [32, 798], [69, 830], [95, 500], ]; var res = array.map(([id, score]) => ({id, score})); console.log(res); 

You can use Array.prototype.map() with destructuring assignment : 您可以将Array.prototype.map()解构赋值一起使用

 const array = [[1, 698],[32, 798],[69, 830],[95, 500]]; const result = array.map(([id, score]) => ({id, score})); console.log(result); 

Use array.prototype.map , destructuring and shorthand object litteral : 使用array.prototype.mapdestructuringshorthand object litteral

 var array = [[1, 698],[32, 798],[69, 830],[95, 500]]; var result = array.map(([id, score]) => ({id, score})); console.log(result); 

var sampleArray = [[1, 698],[32, 798],[69, 830],[95, 500]];

var finalJson = sampleArray.map(([id, score]) => ({id, score}));


// Final Result
console.log(finalJson);

first you need a function that takes a 2 element array and returns an object 首先,您需要一个带有2个元素数组并返回一个对象的函数

const objBuilder = arr => return { id: arr[0], score: arr[1] }

you will want to add error handling, but thats the basic idea. 你会想要添加错误处理,但这是基本的想法。

Next you want to iterate over the array of arrays transforming each value (2 element array) into an object. 接下来,您希望迭代数组数组,将每个值(2个元素数组)转换为对象。 Thats called mapping over values, and js supports it natively 这称为映射值,而js本身支持它

const arrayOfObjects =  array.map(objBuilder)

more about map function here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map 有关map函数的更多信息,请访问: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

The answers from a number of people suggesting .map(([id, score]) => ({id, score})) are great. 许多人的答案表明.map(([id, score]) => ({id, score}))很棒。 But if you have to do things like this often, you might want to write a reusable function to make this more declarative. 但是如果你经常要做这样的事情,你可能想写一个可重用的函数来使它更具说明性。 For that, something like this might work: 为此,这样的事情可能有效:

 const zipObject = names => values => names.reduce( (obj, name, idx) => (obj[name] = values[idx], obj), {} ) const array = [[1, 698], [32, 798], [69, 830], [95, 500]] console.log(array.map(zipObject(['id', 'score']))) 

Note that you could also extend this to 请注意,您也可以将其扩展为

zipAllObjects = names => listsOfValues => listsOfValues.map(zipObject(names))

and just call 然后打电话

zipAllObjects(['id', 'score'])(array)

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

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