简体   繁体   English

在javascript中将JSON对象转换为数组数组

[英]Converting JSON object into array of arrays in javascript

I have tried to take the particular array value of key from a JSON object and store that in an array which will be look like this. 我试图从JSON对象中获取key的特定数组值,并将其存储在一个类似于此的数组中。

Example: 例:

var obj = { "1":{"class":2,"percent":0.99,"box":[0.2,0.3,0.4,0.5]},
            "2":{"class":2,"percent":0.99,"box":[0.12,0.23,0.45,0.56]},
            "3":{"class":2,"percent":0.99,"box":[0.52,0.83,0.34,0.59]}
           } 

and so on like this 等等

Now i need to take the value of key "box" and store in an array. 现在我需要获取键“box”的值并存储在数组中。

var list = []
list = [[0.2,0.3,0.4,0.5],[0.12,0.23,0.45,0.56],[0.52,0.83,0.34,0.59]]

But, i tried multiple ways to store the array inside the array, but i could able to get like this when i printed the list 但是,我尝试了多种方法将数组存储在数组中,但是当我打印列表时,我能够得到这样的结果

list = 0.2,0.3,0.4,0.5,0.12,0.23,0.45,0.56,0.52,0.83,0.34,0.59

You can use Object.keys (which returns an array of all key of your json) in combination with array's map to transform your json into a new array: 您可以使用Object.keys(返回json的所有键的数组)与数组的映射相结合,将您的json转换为新数组:

const boxes = Object.keys(obj).map(key => obj[key].box)

You can also use Object.values, which is actually nicer in your case: 您也可以使用Object.values,在您的情况下实际上更好:

const boxes = Object.values(obj).map(value => value.box)

How it works 这个怎么运作

Object.keys return an array: Object.keys返回一个数组:

Object.keys(obj) // ["1", "2", "3"]

then you can map over them to get the value of each item: 然后你可以映射它们以获得每个项目的值:

Object.keys(obj).map(key => obj[key]) // [{box: [...]}, {box: [...]}, {box: [...]}]

then in array.map 's callback, you can simply only return the box value: 然后在array.map的回调中,你只需返回框值:

Object.keys(obj).map(key => obj[key].box) // [[...], [...], [...]]

Without Object.keys() 没有Object.keys()

function getBoxes (object) {
  var boxes = [];
  for (var key in object) {
    if (!object.hasOwnProperty(key)) continue;
    boxes.push(object[key].box);
  }
  return boxes;
}

console.log(getBoxes(obj))

for...in can loop through object's properties, but it'll also loop over inherited properties, therefore we need to guard the loop with object.hasOwnProperty(key) . for...in可以遍历对象的属性,但它也会遍历继承的属性,因此我们需要使用object.hasOwnProperty(key)来保护循环。


Object.keys() : Return an array of all enumerable property names of an object Object.keys() :返回一个对象的所有可枚举属性名称的数组

Object.values() : Return an array of all enumerable values of an object Object.values() :返回一个对象的所有可枚举值的数组

array.map() : Return a new array with each item of the original array transformed in a given callback function array.map() :返回一个新数组,其中包含在给定回调函数中转换的原始数组的每个项目

array.slice() : Return a shallow copy of the original array array.slice() :返回原始数组的浅表副本

Try this: 尝试这个:

Object.values(obj).map(x=>x.box);

 var obj = { "1":{"class":2,"percent":0.99,"box":[0.2,0.3,0.4,0.5]}, "2":{"class":2,"percent":0.99,"box":[0.12,0.23,0.45,0.56]}, "3":{"class":2,"percent":0.99,"box":[0.52,0.83,0.34,0.59]} } let list = Object.values(obj).map(x=>x.box); console.log(JSON.stringify(list)) 

The Object.values returns array of obj values, then we use map to iterate over that values (we use arrow function ) and create new array with box values. Object.values返回obj值的数组,然后我们使用map迭代这些值(我们使用箭头函数 )并使用box值创建新数组。

Check next example: 检查下一个示例:

1) First, use Object.keys() to get an array with the keys of the object. 1)首先,使用Object.keys()获取包含对象键的数组。

2) Second, loop on every key and access the box property of the object associated with every key, then push this box property on a new array. 2)其次,循环每个key并访问与每个键关联的对象的box属性,然后在新数组上推送此box属性。

 var obj = { "1": {"class":2, "percent":0.99, "box":[0.2,0.3,0.4,0.5]}, "2": {"class":2, "percent":0.99, "box":[0.12,0.23,0.45,0.56]}, "3": {"class":2, "percent":0.99, "box":[0.52,0.83,0.34,0.59]} }; var arrayOfArrays = []; Object.keys(obj).forEach(function(k){ arrayOfArrays.push(obj[k].box); }); console.log(arrayOfArrays); 

Here is (just for fun) another alternative using the reduce() method: 这是(只是为了好玩)另一种使用reduce()方法的替代方法:

 var obj = { "1": {"class":2, "percent":0.99, "box":[0.2,0.3,0.4,0.5]}, "2": {"class":2, "percent":0.99, "box":[0.12,0.23,0.45,0.56]}, "3": {"class":2, "percent":0.99, "box":[0.52,0.83,0.34,0.59]} }; const reducer = (accumulator, currVal) => { accumulator.push(currVal.box); return accumulator; }; arrayOfArrays = Object.values(obj).reduce(reducer, []); console.log(arrayOfArrays); 

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

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