JavaScript排序数组与相同的项[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我正在尝试对数组进行排序。我想对它进行排序,以便对于相同的accountID,我将所有项目存储为数组项目。样本输入:

[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Petrol Charges At Esso"}],
[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Hylo Lubricant Eye Drops 10ml"}],
[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Genteal Gel, Sterile Lubricant Eye Gel, 10g"}],
[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Genteal Gel, Sterile Lubricant Eye Gel, 10g"}],
[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Blink Intensive Tears Protective Eye Drops 0.4mlx20"}],
[{accountID: "-Ks8mWWekpN2BfOFcdbS", itemName: "Palmers White And Even Dark Circles Eye Treatment Cream 15ml"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Sensitive Pro-relief With Whitening Toothpaste 110g"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "2 In 1 Pure Breath Toothpaste"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Antibackterial Mouthwash 200ml"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Akira 1.7l Jug Kettle Jk1718c"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Duracell Alkaline Batteries Aaa 12s"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Osram Led Star Classic A100 Light Bulb - Frosted Warm White 10.5w/827"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Sharks Fin Soup With Crab Meat And Cordyceps"}],
[{accountID: "-Ks8mWWq445Uao_9sgNn", itemName: "Chilli Fried Rice With Shrimps"}],

enter image description here

要打印到文本文件的所需输出:

['Petrol Charges At Esso', 'Hylo Lubricant Eye Drops 10ml', 'Genteal Gel, Sterile Lubricant Eye Gel, 10g', 'Blink Intensive Tears Protective Eye Drops 0.4mlx20', 'Palmers White And Even Dark Circles Eye Treatment Cream 15ml'],
['Sensitive Pro-relief With Whitening Toothpaste 110g', '2 In 1 Pure Breath Toothpaste', 'Antibackterial Mouthwash 200ml', 'Akira 1.7l Jug Kettle Jk1718c', 'Duracell Alkaline Batteries Aaa 12s', 'Osram Led Star Classic A100 Light Bulb - Frosted Warm White 10.5w/827', 'Sharks Fin Soup With Crab Meat And Cordyceps', 'Chilli Fried Rice With Shrimps'],

我的JavaScript代码:

// for simplicity purpose, I do not post the chunk where I resolve the promise. 
promiseKey.then((arr) => {
console.log(arr);
            var result = arr.reduce(function(items, item) {
                var existing = items.find(function(i) {
                    return i.accountID === item.accountID;
                });

                if (existing) {
                    existing.filteredlist.push(item.itemName);
                } else {
                    items.push(item);
                }
            return items;
            }, []);
            console.log('filtered');
            console.log(result);
});

我得到的错误是Uncaught(在promise中)TypeError:无法在else语句中读取未定义的属性'push'。

javascript arrays sorting
2个回答
2
投票

您可以使用foreach循环和对象采用更简单的方法。

var items = {};

arr.forEach(function(item) {

    if(!items.hasOwnProperty(item.accountID)) {
        items[item.accountID] = [];
    }

    items[item.accountID].push(item.itemName);
});

您可以使用Object.keys(items)获取每个帐户的密钥。

Object.keys(items).forEach(function(account) {
    // account is the accountID

    var accountItems = items[account];
});

1
投票

在这里你需要在filteredlist使用之前初始化

更新的代码

console.log(arr);
            var result = arr.reduce(function(items, item) {
                var existing = items.find(function(i) {
                    return i.accountID === item.accountID;
                });

                if (existing) {                  
                   existing.filteredlist = [];
                    existing.filteredlist.push(item.itemName);
                } else {
                    items.push(item);
                }
            return items;
            }, []);
            console.log('filtered');
            console.log(result);

这是我的jsbin demo https://jsbin.com/tepedut/edit?js,console

编辑:

根据您的评论,您需要使用arr属性对accountID进行排序,您可以使用javascript arr.sort(function(a, b){});函数对数组进行排序。

这是更新的代码

arr.sort(function(a, b){
   if(a.accountID < b.accountID) return -1;
    if(a.accountID > b.accountID) return 1;
    return 0;
});

console.log(arr);

更新了js bin https://jsbin.com/tepedut/8/edit?js,console

© www.soinside.com 2019 - 2024. All rights reserved.