如何将json值过滤到数组

时间:2019-01-07 07:40:25

标签: javascript jquery json object

我有一个像这样的json文件

let myfile = [
    {"name":"John","eid":664,"socialid":399,"testid":799},
    {"name":"Sam","testid":249,"eid":64,"socialid":80},
    {"name":"Albert","eid":422,"testid":20,"socialid":10},
    {"name":"Michel","eid":497,"testid":15,"socialid":60}]
从上面的json中

我想通过其键名过滤所有值并将其推送到数组。 预期输出为:

"name": ["John", "Sam", "Albert", "Michel"],
"eid": [664, 64, 422, 497],
"testid": [799, 249, 20, 15],
"socialid": [399, 80, 10, 60]

如何做到这一点?

我尝试过这样

let arr3 = [];
$.each( myfile, function( key, value ) {
  if(this.hasOwnProperty('name'))
  {
    console.log("is there")
    arr3.push(value);
  }
});
console.log(arr3);

它没有按预期工作。

4 个答案:

答案 0 :(得分:1)

您可以将数组简化为一个对象:

let myfile = [
    { name: 'John', eid: 664, socialid: 399, testid: 799 },
    { name: 'Sam', testid: 249, eid: 64, socialid: 80 },
    { name: 'Albert', eid: 422, testid: 20, socialid: 10 },
    { name: 'Michel', eid: 497, testid: 15, socialid: 60 },
];

console.log(
    myfile.reduce(
        (result, item) =>
            Object.entries(item).reduce((result, [key, value]) => {
                result[key] = result[key] || [];
                result[key].push(value);
                return result;
            }, result),
        {},
    ),
);

答案 1 :(得分:1)

您可以使用array#reduce来累积与对象中每个键相对应的值。

let myfile = [{"name":"John","eid":664,"socialid":399,"testid":799}, {"name":"Sam","testid":249,"eid":64,"socialid":80}, {"name":"Albert","eid":422,"testid":20,"socialid":10}, {"name":"Michel","eid":497,"testid":15,"socialid":60}],
    result = myfile.reduce((r,o) => {
      Object.entries(o).forEach(([key,value]) => {
        r[key] = r[key] || [];
        r[key].push(value);
      });
      return r;
    },{});
console.log(result);

答案 2 :(得分:1)

您的主要问题是,您尝试访问内部对象中不存在的publication

对于内联重命名,可以将具有给定名称的对象作为键,将新名称作为值,并将实际键作为默认值。然后创建一个新数组(如果不存在)并将其值推送到它。

最后,您将获得一个带有所需键的对象,该键具有来自给定数据的所有值。

var myfile = [{ "name": "John", "eid": 664, "socialid": 399, "testid": 799 }, { "name": "Sam", "testid": 249, "eid": 64, "socialid": 80 }, { "name": "Albert", "eid": 422, "testid": 20, "socialid": 10 }, { "name": "Michel", "eid": 497, "testid": 15, "socialid": 60 }],
    replace = { name: 'publication' },
    result = myfile.reduce((r, o) => Object.entries(o).reduce((p, [k, v]) => {
        k = replace[k] || k;
        (p[k] = p[k] || []).push(v);        
        return p;
    }, r), Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

喜欢吗?

let json=[
    {"name":"John","eid":664,"socialid":399,"testid":799},
    {"name":"Sam","testid":249,"eid":64,"socialid":80},
    {"name":"Albert","eid":422,"testid":20,"socialid":10},
    {"name":"Michel","eid":497,"testid":15,"socialid":60}
]
let output={
  publication:[],
  eid:[],
  testid:[],
  socialid:[]
}
for(let i in json){
    output.publication.push(json[i].name)
    output.eid.push(json[i].eid)
    output.testid.push(json[i].testid)
    output.socialid.push(json[i].socialid)
}
console.log(output)

我希望这会对您有所帮助!