[英]Javascript Array of object join duplicates
我需要按國家/地區代碼查找重復項並將它們合並為一個對象
數組結構為:
[{...},{...}]
對象
{Country_Code: "RU", Country: "Russia", Provider: "Shell1", Price: "0.123"},
{Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856"},
{Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell2", Price: "0.90890"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell3", Price: "0.90"}
我需要獲取對象數組
[{Country_Code: "RU", Country: "Russia", Provider: "Shell1 - 0.123</br>Shell2 - 0.90890</br>Shell3 - 0.90", Price: "0.123"},
{Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856"},
{Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563"}]
如果想必一定是這樣
var NewArr =[],NewObj ={};
data.forEach(function(dataItem,index) {
CC = dataItem.Country_Code;
counts[CC]= (counts[CC]||0) + 1; // Count duplicates
if(counts[CC]>1){
NewObj.Country_Code = dataItem.Country_Code;
NewObj.Country = dataItem.Country;
NewObj.Provider = dataItem.Provider + " - " + dataItem.Price + "</br>";
NewObj.Price = dataItem.Price;
NewArr[indexOfDup] = NewObj; //but how to know index?
}else{
NewArr.push(data[index]);
}
});
但這是錯誤的,那么如何編寫正確的語法呢?
您將需要array.reduce
。 使用array.reduce返回一個數組。 在此數組中,使用findIndex
檢查是否存在country_code存在的對象。 如果存在,則更新屬性值
let x = [{ Country_Code: "RU", Country: "Russia", Provider: "Shell1", Price: "0.123" }, { Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856" }, { Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563" }, { Country_Code: "RU", Country: "Russia", Provider: "Shell2", Price: "0.90890" }, { Country_Code: "RU", Country: "Russia", Provider: "Shell3", Price: "0.90" } ] let m = x.reduce(function(acc, curr) { let findIndex = acc.findIndex((item) => { return item['Country_Code'] === curr['Country_Code'] }); if (findIndex === -1) { acc.push(curr) } else { acc[findIndex].Provider = acc[findIndex].Provider + ' - ' + acc[findIndex].Price + '<br/>' + curr.Provider + ' - ' + curr.Price } return acc; }, []); console.log(m)
'use strict';
const data = [
{Country_Code: "RU", Country: "Russia", Provider: "Shell1", Price: "0.123"},
{Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856"},
{Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell2", Price: "0.90890"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell3", Price: "0.90"}
];
// create a hashmap by country code
const dataPerCountry = data.reduce((dataCountry, currentValue) => {
const item = { }
if (dataCountry[currentValue.Country_Code]) {
dataCountry[currentValue.Country_Code].Provider.push(currentValue.Provider);
} else {
dataCountry[currentValue.Country_Code] = {
...currentValue,
Provider: [currentValue.Provider],
};
}
return dataCountry;
}, {});
/**
* Transform the hasmap into an array of objects used by the view,
* also applying the transformation to concat the Providers
**/
const reducedCountriesVO = Object.keys(dataPerCountry).map((countryCode) => {
const dataCountry = { ...dataPerCountry[countryCode] };
dataCountry.Provider = dataCountry.Provider.join('<br>-');
return dataCountry;
});
console.log(reducedCountriesVO);
有很多解決方法。 這是我的小貢獻,我提出的算法不使用任何搜索算法。 因此,對於大型陣列,它可以相對快速地執行。
var myArray = [{Country_Code: "RU", Country: "Russia", Provider: "Shell1", Price: "0.123"},
{Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856"},
{Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell2", Price: "0.90890"},
{Country_Code: "RU", Country: "Russia", Provider: "Shell3", Price: "0.90"}];
var groupByCountryCode = function(xs, key) {
return xs.reduce(function(rv, x) {
if(rv[x[key]]){
// It has found more than one instance
rv[x[key]][0].Provider += " " + x.Provider;
}else{
rv[x[key]] = [];
rv[x[key]].push(x);
}
return rv;
}, {});
};
var grouppedElementsByKey = groupByCountryCode(myArray,"Country_Code");
var grouppedArray = Object.keys(grouppedElementsByKey).map(function(key){
return grouppedElementsByKey[key][0];
});
console.log(grouppedArray);
最終輸出將像您所要求的那樣。
這是使用reduce
一種解決方案。 首先,我使用具有多個值的屬性的數組創建了一個地圖結構。 然后,我使用有組織的結構來格式化數據,使其看起來像您想要的樣子:
var data = [{ Country_Code: "RU", Country: "Russia", Provider: "Shell1", Price: "0.123" }, { Country_Code: "EN", Country: "Russia", Provider: "Shell", Price: "0.76856" }, { Country_Code: "RO", Country: "Russia", Provider: "Shell", Price: "0.563" }, { Country_Code: "RU", Country: "Russia", Provider: "Shell2", Price: "0.90890" }, { Country_Code: "RU", Country: "Russia", Provider: "Shell3", Price: "0.90" }]; //First, make a map structure for organization var map = data.reduce((map, el) => { let provider = []; let price = []; if (map[el.Country_Code]) { provider = map[el.Country_Code].Provider; price = map[el.Country_Code].Price; } provider.push(el.Provider); price.push(el.Price); map[el.Country_Code] = { Country_Code: el.Country_Code, Country: el.Country, Provider: provider, Price: price } return map; }, {}); //Use the data map to display the data however you want var formatted = Object.values(map).map((el) => { return { Country_Code: el.Country_Code, Country: el.Country, Provider: el.Provider.reduce((str, providerEl, idx) => { return str += `${providerEl} - ${el.Price[idx]}</br>`; }, ''), Price: el.Price[0] //this doesnt make sense IMO, but will leave it since that's how you asked for the data } }); console.log(Object.values(formatted)); //{Country_Code: "RU", Country: "Russia", Provider: "Shell1 - 0.123</br>Shell2 - 0.90890</br>Shell3 - 0.90", Price: "0.123"}
您可以在此處使用use array.indexOf(someValue)
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.