【问题标题】:how to filter json values to an array [closed]如何将json值过滤到数组中[关闭]
【发布时间】:2019-01-07 07:40:25
【问题描述】:

我有一个这样的 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);

它没有按预期工作。

【问题讨论】:

  • @Meeravali 你试过什么?
  • 另外,您想要的输出不明确。你说你想要一个数组,但这看起来像一个对象(有点)。

标签: javascript jquery json object


【解决方案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】:

    您可以将数组缩减为一个对象:

    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),
            {},
        ),
    );

    【讨论】:

      【解决方案3】:

      您的主要问题是,您尝试访问内部对象中不存在的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; }

      【讨论】:

        【解决方案4】:

        像这样?

        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)

        希望对你有帮助!

        【讨论】:

        • 为什么不for(let i in json){
        猜你喜欢
        • 2016-09-02
        • 2021-11-02
        • 2013-03-07
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        相关资源
        最近更新 更多