假设我们有对象的初始数组:
{vendor:"vendor1", item:"item1", price:1100, rank:0},
{vendor:"vendor1", item:"item2",price:3200, rank:0},
{vendor:"vendor1", item:"item3", price:1100, rank:0},
{vendor:"vendor2", item:"item1", price:2000, rank:0},
{vendor:"vendor2", item:"item2",price:2000, rank:0},
{vendor:"vendor2", item:"item3", price:3200, rank:0},
{vendor:"vendor3", item:"item1", price:3200, rank:0},
{vendor:"vendor3", item:"item2",price:1100, rank:0},
{vendor:"vendor3", item:"item3", price:2000, rank:0},
与所有具有相同项目的供应商相比,如何根据其项目类别填充基于价格属性的排名。
所需结果:
{vendor:"vendor1", item:"item1", price:1100, rank:1},
{vendor:"vendor1", item:"item2",price:3200, rank:3},
{vendor:"vendor1", item:"item3", price:1100, rank:1},
{vendor:"vendor2", item:"item1", price:2000, rank:2},
{vendor:"vendor2", item:"item2",price:2000, rank:2},
{vendor:"vendor2", item:"item3", price:3200, rank:3},
{vendor:"vendor3", item:"item1", price:3200, rank:3},
{vendor:"vendor3", item:"item2",price:1100, rank:1},
{vendor:"vendor3", item:"item3", price:2000, rank:2},
答案 0 :(得分:2)
STEPS:
let arr = [{vendor:"vendor1", item:"item1", price:1000, rank:0},
{vendor:"vendor1", item:"item2",price:3000, rank:0},
{vendor:"vendor1", item:"item3", price:1000, rank:0},
{vendor:"vendor2", item:"item1", price:2000, rank:0},
{vendor:"vendor2", item:"item2",price:2000, rank:0},
{vendor:"vendor2", item:"item3", price:3000, rank:0},
{vendor:"vendor3", item:"item1", price:3000, rank:0},
{vendor:"vendor3", item:"item2",price:1000, rank:0},
{vendor:"vendor3", item:"item3", price:2000, rank:0},]
let items = [...new Set(arr.map(o => o.item))]
let resultArr = []
items.forEach(item => {
let filteredArr = arr.filter(o => o.item === item)
filteredArr.sort((a,b) => (a.price > b.price) ? 1 : ((b.price > a.price) ? -1 : 0))
for(let i = 0; i < filteredArr.length; i++){
filteredArr[i].rank = i + 1
}
resultArr = resultArr.concat(filteredArr)
})
resultArr.sort((a,b) => (a.vendor > b.vendor) ? 1 : ((b.vendor > a.vendor) ? -1 : 0))
console.log(resultArr)
答案 1 :(得分:0)
尝试此.rank是根据价格值创建的
var arr = [{vendor:"vendor1", item:"item1", price:1000, rank:0}, {vendor:"vendor1", item:"item2",price:1000, rank:0}, {vendor:"vendor1", item:"item3", price:1000, rank:0}, {vendor:"vendor2", item:"item1", price:2000, rank:0}, {vendor:"vendor2", item:"item2",price:2000, rank:0}, {vendor:"vendor2", item:"item3", price:2000, rank:0}, {vendor:"vendor3", item:"item1", price:3000, rank:0}, {vendor:"vendor3", item:"item2",price:3000, rank:0}, {vendor:"vendor3", item:"item3", price:3000, rank:0}];
arr.forEach(a => a.rank = (a.price/1000))
console.log(arr)
答案 2 :(得分:0)
您可以收集分组的price
,对其进行排序,然后将更正后的索引应用为rank
。
var data = [{ vendor: "vendor1", item: "item1", price: 1100, rank: 0 }, { vendor: "vendor1", item: "item2", price: 3200, rank: 0 }, { vendor: "vendor1", item: "item3", price: 1100, rank: 0 }, { vendor: "vendor2", item: "item1", price: 2000, rank: 0 }, { vendor: "vendor2", item: "item2", price: 2000, rank: 0 }, { vendor: "vendor2", item: "item3", price: 3200, rank: 0 }, { vendor: "vendor3", item: "item1", price: 3200, rank: 0 }, { vendor: "vendor3", item: "item2", price: 1100, rank: 0 }, { vendor: "vendor3", item: "item3", price: 2000, rank: 0 }],
collection = data.reduce((r, o) => ((r[o.item] = r[o.item] || []).push(o.price), r), {});
Object.values(collection).forEach(a => a.sort((a, b) => a - b));
data.forEach(o => o.rank = collection[o.item].indexOf(o.price) + 1);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 3 :(得分:0)
您可以基于item
和price
sort
数组。然后,循环遍历已排序的数组,并根据先前的rank
与当前的item
是否相同来分配item
:
const arr = [{vendor:"vendor1",item:"item1",price:1100,rank:0},{vendor:"vendor1",item:"item2",price:3200,rank:0},{vendor:"vendor1",item:"item3",price:1100,rank:0},{vendor:"vendor2",item:"item1",price:2000,rank:0},{vendor:"vendor2",item:"item2",price:2000,rank:0},{vendor:"vendor2",item:"item3",price:3200,rank:0},{vendor:"vendor3",item:"item1",price:3200,rank:0},{vendor:"vendor3",item:"item2",price:1100,rank:0},{vendor:"vendor3",item:"item3",price:2000,rank:0}]
const output = arr.sort((a, b) => a.item.localeCompare(b.item) || a.price - b.price)
.map((o, i) => {
const prev = arr[arr.length - 1]
const rank = prev && prev.item === o.item
? prev.rank + 1
: 1;
return { ...o, rank }
})
console.log(output)
答案 4 :(得分:-1)
您可以使用 map 函数
var arr = [{vendor:"vendor1", item:"item1", price:1000, rank:0},
{vendor:"vendor1", item:"item2",price:3000, rank:0},
{vendor:"vendor1", item:"item3", price:1000, rank:0},
{vendor:"vendor2", item:"item1", price:2000, rank:0},
{vendor:"vendor2", item:"item2",price:2000, rank:0},
{vendor:"vendor2", item:"item3", price:3000, rank:0},
{vendor:"vendor3", item:"item1", price:3000, rank:0},
{vendor:"vendor3", item:"item2",price:1000, rank:0},
{vendor:"vendor3", item:"item3", price:2000, rank:0}];
arr.map(a => a.rank = (a.price/1000))
console.log(arr)
或使用 forEach 函数
var arr = [{vendor:"vendor1", item:"item1", price:1000, rank:0},
{vendor:"vendor1", item:"item2",price:3000, rank:0},
{vendor:"vendor1", item:"item3", price:1000, rank:0},
{vendor:"vendor2", item:"item1", price:2000, rank:0},
{vendor:"vendor2", item:"item2",price:2000, rank:0},
{vendor:"vendor2", item:"item3", price:3000, rank:0},
{vendor:"vendor3", item:"item1", price:3000, rank:0},
{vendor:"vendor3", item:"item2",price:1000, rank:0},
{vendor:"vendor3", item:"item3", price:2000, rank:0}];
arr.forEach(a => a.rank = (a.price/1000))
console.log(arr)