将嵌套的JSON值推送到数组

时间:2019-01-23 17:57:44

标签: javascript json

我有以下类型的JSON数组:

"team": [ 
          {
            "paid": {
                "refugee": 2018,
                  "local": 29000, 
                  "international": 12000
            }
        },
        {
            "unpaid": {
                "refugee": 2019,
                "local": 39000, 
                "international": 19000
            }
        }
    ]

我想将匹配键的值推入数组,以便最终得到以下新数组:

var refugees = [2018, 2019]
var local = [29000, 39000]
var international = [12000, 19000]

以此类推。

执行此操作的简单方法是什么?过去,我已经成功使用jQuery,但是需要仅使用Javascript的解决方案:

$.each(team, function (i, v) {
                var teams = v;
                console.log(teams);
                $.each(v, function (i, v) {
                    refugees.push(v.refugee);
                    local.push(v.local);
                    international.push(v.international);
                });
            });

5 个答案:

答案 0 :(得分:1)

尝试一下

var a={"team" : [ 
  {
    "paid": {
        "refugee": 2018,
          "local": 29000, 
          "international": 12000
    }
},

{
    "unpaid": {
        "refugee": 2019,
        "local": 39000, 
        "international": 19000
    }
}
]}
var refugee=[];
var local=[];
var international=[];
a.team.map((e)=>{
  if(e.paid)
  {
refugee.push(e.paid.refugee);
local.push(e.paid.local);
international.push(e.paid.international)
  }
  else
  {
    refugee.push(e.unpaid.refugee);
local.push(e.unpaid.local);
international.push(e.unpaid.international)
  }

})
console.log(local)
console.log(international)
console.log(refugee)

答案 1 :(得分:0)

您可以使用reduce。

因此,这里的想法是我们获取一个键并将其映射到输出对象。我们一直检查键是否已经在输出对象中,我们将值推入该特定键,如果不是,则添加一个带有value的新属性。

let obj = {"team":[{"paid":{"refugee":2018,"local":29000,"international":12000}},{"unpaid":{"refugee":2019,"local":39000,"international":19000}}]}

 let op = obj.team.reduce((output,current)=>{
  let temp = Object.values(current)[0]
  let values = Object.keys(temp)
  values.forEach(ele=>{
    if(output[ele]){
      output[ele].push(temp[ele])
    } else {
      output[ele] = [temp[ele]]
    }
  })
  return output;
 }, {})
 
 console.log(op)

答案 2 :(得分:0)

如果您需要一些单线,这样的事情会起作用:

let local = team.reduce((acc, item) => acc.concat(Object.values(item).map(val => val.local)), []);

let refugee = team.reduce((acc, item) => acc.concat(Object.values(item).map(val => val.refugee)), []);

答案 3 :(得分:-1)

以最简单的形式,您可以获取对象属性的正确路径并进行设置。在您的用例中,很有可能不会那么简单。您极有可能需要使用某种方法来找到要寻找的对象,然后在此处设置它的形式。 .find.filter.includes是用于在数组中搜索内容的出色工具。

这里还要注意的一件事是,refugeelocal的当前值不是数组格式。话虽如此,.push并不是一个选择。您首先需要将属性值设置为数组,然后可以使用Array.push

const json = {"team": [ 
          {
            "paid": {
                "refugee": 2018,
                  "local": 29000, 
                  "international": 12000
            }
        },

        {
            "unpaid": {
                "refugee": 2019,
                "local": 39000, 
                "international": 19000
            }
        }
    ]};
json.team[0].paid.refugee = [2018, 2019];
json.team[0].paid.local = [2900, 3900];

console.log(json);

答案 4 :(得分:-1)

使用Array#reduce,Object#values,Object#entries,扩展语法,解构和Map

const data={"team":[{"paid":{"refugee":2018,"local":29000,"international":12000}},{"unpaid":{"refugee":2019,"local":39000,"international":19000}}]}

const res = data.team.reduce((a,c)=>{
  Object.values(c)
  .map(Object.entries)
  .flat()
  .forEach(([k,v])=>{
    const arr = a.get(k) || [];
    arr.push(v)
    a.set(k, arr);
  })
  return a;
}, new Map())

//get all
console.log([...res.values()]);
//get by type
console.log(res.get('refugee'));
console.log(res.get('local'));
console.log(res.get('international'));