JavaScript数组对象按值复制

时间:2016-04-01 15:41:51

标签: javascript arrays

我有一个对象数组:

var allApps = [
    { 
        App: "Google", 
        Descr: "searching", 
        ExpiryDate: "2026-03-04T00:00:00" 
    },
    { 
        App: "Facebook", 
        Descr: "social network", 
        ExpiryDate: "2021-02-07T00:00:00" 
    },
    { 
        App: "Amazon", 
        Descr: "sales", 
        ExpiryDate: "2024-04-09T00:00:00" 
    },
    { 
        App: "Google", 
        Descr: "maps", 
        ExpiryDate: "2026-03-04T00:00:00" 
    },
    { 
        App: "Amazon", 
        Descr: "notebooks", 
        ExpiryDate: "2024-04-09T00:00:00" 
    },
    { 
        App: "Google", 
        Descr: "video", 
        ExpiryDate: "2055-03-04T00:00:00" 
    },
    { 
        App: "Google", 
        Descr: "maps and social", 
        ExpiryDate: "2026-03-04T00:00:00" 
    },
    { 
        App: "Amazon", 
        Descr: "notebooks", 
        ExpiryDate: "2035-04-09T00:00:00" 
    },
    { 
        App: "Amazon", 
        Descr: "mp3", 
        ExpiryDate: "2035-04-09T00:00:00" 
    }
];

我想接收一个多维对象数组,其中每个元素(数组)包含对象,其中值“App”和“ExpiryDate”应该彼此相同,但值“Descr”可以是不同的或等于。每个数组必须至少包含两个元素:

var appsFiltered = [
    [
        { 
            App: "Google", 
            Descr: "searching", 
            ExpiryDate: "2026-03-04T00:00:00" 
        },
        { 
            App: "Google", 
            Descr: "maps", 
            ExpiryDate: "2026-03-04T00:00:00" 
        },
        { 
            App: "Google", 
            Descr: "maps and social", 
            ExpiryDate: "2026-03-04T00:00:00" 
        }
    ],
    [
        { 
            App: "Amazon", 
            Descr: "sales", 
            ExpiryDate: "2024-04-09T00:00:00" 
        },
        { 
            App: "Amazon", 
            Descr: "notebooks", 
            ExpiryDate: "2024-04-09T00:00:00" 
        }
    ],
    [
        { 
            App: "Amazon", 
            Descr: "notebooks", 
            ExpiryDate: "2035-04-09T00:00:00" 
        },
        { 
            App: "Amazon", 
            Descr: "mp3", 
            ExpiryDate: "2035-04-09T00:00:00" 
        }
    ]
];

我怎么做? 感谢

3 个答案:

答案 0 :(得分:0)

您需要一个双循环来过滤掉您的商品。

var i, j, filteredIndexes = [], foundItems, appsFiltered = [];
for(i = 0; i < allApps.length; i++)
{
    if (filteredIndexes.indexOf(i) < 0)
    {
      item = allApps[i];
      foundItems = [item];
      for(j = 0; j < allApps.length; j++)
      {
        if (allApps[j].App === item.App && allApps[j].ExpiryDate === item.ExpiryDate)
        {
          foundItems[foundItems.length] = allApps[j];
          filteredIndexes[filteredIndexes.length] = j;
        }
      }
      appsFiltered[appsFiltered.length] = foundItems;
    }
}
console.log(appsFiltered);

答案 1 :(得分:0)

使用几个reduce函数。

function mergeByHash(p, c) {

  // create a unique key from the App and
  // ExpiryDate properties
  var key = [c.App, '#', c.ExpiryDate].join('');

  // Create a new array if the key doesn't exist
  // otherwise push the object to the array
  p[key] = p[key] || [];
  p[key].push(c);
  return p;
}

function moreThanOne(p, c) {

  // if the array of the hashed property has
  // more than one element add it to the final array
  if (tmp[c].length > 1) p.push(tmp[c]);
  return p;
}

var tmp = allApps.reduce(mergeByHash, {});
var out = Object.keys(tmp).reduce(moreThanOne, []);

输出

[
  [
    {
      "App": "Google",
      "Descr": "searching",
      "ExpiryDate": "2026-03-04T00:00:00"
    },
    {
      "App": "Google",
      "Descr": "maps",
      "ExpiryDate": "2026-03-04T00:00:00"
    },
    {
      "App": "Google",
      "Descr": "maps and social",
      "ExpiryDate": "2026-03-04T00:00:00"
    }
  ],
  [
    {
      "App": "Amazon",
      "Descr": "sales",
      "ExpiryDate": "2024-04-09T00:00:00"
    },
    {
      "App": "Amazon",
      "Descr": "notebooks",
      "ExpiryDate": "2024-04-09T00:00:00"
    }
  ],
  [
    {
      "App": "Amazon",
      "Descr": "notebooks",
      "ExpiryDate": "2035-04-09T00:00:00"
    },
    {
      "App": "Amazon",
      "Descr": "mp3",
      "ExpiryDate": "2035-04-09T00:00:00"
    }
  ]
]

DEMO

答案 2 :(得分:0)

这是一个带有一个循环和一个临时对象的提案,用于引用结果数组。

var allApps = [{ App: "Google", Descr: "searching", ExpiryDate: "2026-03-04T00:00:00" }, { App: "Facebook", Descr: "social network", ExpiryDate: "2021-02-07T00:00:00" }, { App: "Amazon", Descr: "sales", ExpiryDate: "2024-04-09T00:00:00" }, { App: "Google", Descr: "maps", ExpiryDate: "2026-03-04T00:00:00" }, { App: "Amazon", Descr: "notebooks", ExpiryDate: "2024-04-09T00:00:00" }, { App: "Google", Descr: "video", ExpiryDate: "2055-03-04T00:00:00" }, { App: "Google", Descr: "maps and social", ExpiryDate: "2026-03-04T00:00:00" }, { App: "Amazon", Descr: "notebooks", ExpiryDate: "2035-04-09T00:00:00" }, { App: "Amazon", Descr: "mp3", ExpiryDate: "2035-04-09T00:00:00" }],
    result = [];

allApps.forEach(function (a) {
    var k = a.App + '|' + a.ExpiryDate;
    if (!(k in this)) {
        this[k] = [];
        result.push(this[k]);
    }
    this[k].push(a);
}, {});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');