【问题标题】:split an array with object to 2 arrays将带有对象的数组拆分为 2 个数组
【发布时间】:2018-02-25 14:55:24
【问题描述】:

我有一个包含多个对象的数组。我想将此数组拆分为多个数组。要拆分的条件是项目continent

MyArray = [{continent:"europe", fruit:"orange", value:2},
           {continent:"asia", fruit:"banana", value:2},
           {continent:"europe", fruit:"apple", value:2},
           {continent:"asia", fruit:"apple", value:5}
          ];

输出:

[
 [{continent:"europe", fruit:"orange", value:2},
  {continent:"europe", fruit:"apple" value:2}
 ], [
  {continent:"asia", fruit:"banana", value:2},
  {continent:"asia", fruit:"apple" value:5}]
];

【问题讨论】:

    标签: javascript arrays object underscore.js


    【解决方案1】:

    您可以搜索具有相同大陆的数组并更新此数组或使用实际对象推送一个新数组。

    var array = [{ continent: "europe", fruit: "orange", value: 2 }, { continent: "asia", fruit: "banana", value: 2 }, { continent: "europe", fruit: "apple", value: 2 }, { continent: "asia", fruit: "apple", value: 5 }],
        grouped = array.reduce(function (r, o) {
            var group = r.find(([{ continent }]) => continent === o.continent)
            if (group) {
                group.push(o);
            } else {
                r.push([o]);
            }
            return r;
        }, []);
        
    console.log(grouped);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      获取大陆,使用filter获取匹配元素

      var MyArray = [{
        continent: "europe",
        fruit: "orange",
        value: 2
      }, {
        continent: "asia",
        fruit: "banana",
        value: 2
      }, {
        continent: "europe",
        fruit: "apple",
        value: 2
      }, {
        continent: "asia",
        fruit: "apple",
        value: 5
      }];
      var getAllContinents = [];
       // getting the unique continent name
      MyArray.forEach(function(item) {
        // if the continent  name is not present then push it in the array
        if (getAllContinents.indexOf(item.continent) === -1) {
          getAllContinents.push(item.continent)
        }
      })
      var modifiedArray = [];
      //iterate over the continent array and find the matched elements where 
      // continent name is same
      getAllContinents.forEach(function(item) {
        modifiedArray.push(MyArray.filter(function(cont) {
          return cont.continent === item
        }))
      })
      console.log(modifiedArray)

      【讨论】:

        【解决方案3】:

        您还可以使用具有指定属性值的键创建对象。这将允许您非常轻松地访问所需的数组。

        你可以使用:

        示例:

        let data = [{continent:"europe", fruit:"orange", value:2},      
                    {continent:"asia", fruit:"banana", value:2},
                    {continent:"europe", fruit:"apple", value:2},
                    {continent:"asia", fruit:"apple", value:5}];
        
        let result = data.reduce((acc, obj) => {
          acc[obj.continent] = acc[obj.continent] || [];
          acc[obj.continent].push(obj);
          return acc;
        }, {});
        
        console.log(result);
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 2015-10-16
          • 2019-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-28
          • 1970-01-01
          • 2014-03-11
          相关资源
          最近更新 更多