首页 > 解决方案 > 根据javascript中的出现将新值添加到对象数组中

问题描述

嗨,我有一个对象数组,我需要根据出现在数组中包含新的属性或值

例如:在对象数组中有一个名为 order number 的属性,如果 object 包含所有对象中的最高 orderNumber,则添加名为 color:red 的新属性(理解的高风险),如果 object 包含所有对象中的第二大数字保留第一个最高的然后属性称为颜色:绿色(中等风险),如果所有对象中第三高的数字,则属性称为颜色:黄色(低风险),如果 orderNumber 相同,则提到的新属性值将根据发生情况相同,其余值将具有名为 color:gray 的属性。(无风险)

这是数组:

data = [{
    id: 181,
    name: null,
    orderNumber: 1
  },
  {
    id: 182,
    name: null,
    orderNumber: 1
  },
  {
    id: 183,
    name: null,
    orderNumber: 2
  },
  {
    id: 184,
    name: null,
    orderNumber: 3
  },
  {
    id: 185,
    name: null,
    orderNumber: 3
  },
  {
    id: 186,
    name: null,
    orderNumber: 4
  },
  {
    id: 188,
    name: null,
    orderNumber: 4
  },
  {
    id: 189,
    name: null,
    orderNumber: 4
  },
  {
    id: 190,
    name: null,
    orderNumber: 5
  },
  {
    id: 191,
    name: null,
    orderNumber: 6
  },
]

上面的数组中需要与 orderNumber 值进行比较,

下面是O/P:

data = [{
    id: 181,
    name: null,
    orderNumber: 1,
    color: gray
  },
  {
    id: 182,
    name: null,
    orderNumber: 1,
    color: gray
  },
  {
    id: 183,
    name: null,
    orderNumber: 2,
    color: gray
  },
  {
    id: 184,
    name: null,
    orderNumber: 3,
    color: gray
  },
  {
    id: 185,
    name: null,
    orderNumber: 3,
    color: gray
  },
  {
    id: 186,
    name: null,
    orderNumber: 4,
    color: yellow
  },
  {
    id: 188,
    name: null,
    orderNumber: 4,
    color: yellow
  },
  {
    id: 189,
    name: null,
    orderNumber: 4,
    color: yellow
  },
  {
    id: 190,
    name: null,
    orderNumber: 5,
    color: green
  },
  {
    id: 191,
    name: null,
    orderNumber: 6,
    color: red
  },
]

在上面的输出中,orderNumber: 6 是红色的,因为它是最大的,而 orderNumber: 5 是绿色的,因为它是第二高的,orderNumber: 4 相同,其余为灰色,只需要为第一个分配三种颜色三个最高值,其余颜色为灰色。

应该是递减顺序。

标签: javascriptangular

解决方案


您可以使用Math.max()找到最高阶数。现在检查与当前订单号的差异,并根据数组索引应用该颜色。

该解决方案使用颜色数组来添加它的颜色。如果您增加颜色的数量,它将显示在输出中。

const data=[{id:181,name:null,orderNumber:1},{id:182,name:null,orderNumber:1},{id:183,name:null,orderNumber:2},{id:184,name:null,orderNumber:3},{id:185,name:null,orderNumber:3},{id:186,name:null,orderNumber:4},{id:188,name:null,orderNumber:4},{id:189,name:null,orderNumber:4},{id:190,name:null,orderNumber:5},{id:191,name:null,orderNumber:6}];

const applyColor = (arr, colors) => {
  // Get highest number
  const highestNum = arr.reduce((high, item) => item.orderNumber > high ? item.orderNumber : high, 0);
  // Get last color
  const lastColor = colors.pop();
  // Amount of other colors
  const totalColors = colors.length - 1;
  
  // Map a color to each item
  return arr.map(item => {
    // Get the index of the color
    const index = highestNum  - item.orderNumber;
    // If the index is to high, add the last color else add the color at that index
    item.color = index > totalColors ? lastColor : colors[index];
    
    return {...item};
  });
}

// Adding more colors will change the output
const colors1 = ["red", "green", "yellow", "gray"];
const result1 = applyColor(data, colors1);

const colors2 = ["red", "green", "yellow", "orange", "grey"];
const result2 = applyColor(data, colors2);

console.log(result1);
console.log(result2);

暂无评论:

const data=[{id:181,name:null,orderNumber:1},{id:182,name:null,orderNumber:1},{id:183,name:null,orderNumber:2},{id:184,name:null,orderNumber:3},{id:185,name:null,orderNumber:3},{id:186,name:null,orderNumber:4},{id:188,name:null,orderNumber:4},{id:189,name:null,orderNumber:4},{id:190,name:null,orderNumber:5},{id:191,name:null,orderNumber:6}];

const applyColor = (arr, colors) => {
  const allOrderNums = arr.map(item => item.orderNumber);
  const highestNum = Math.max(...allOrderNums);
  
  const lastColor = colors.pop();
  const totalColors = colors.length - 1;
  
  return arr.map(item => {
    const index = highestNum  - item.orderNumber;
    item.color = index > totalColors ? lastColor : colors[index];
    
    return {...item};
  });
}

const colors1 = ["red", "green", "yellow", "gray"];
const result1 = applyColor(data, colors1);

const colors2 = ["red", "green", "yellow", "orange", "grey"];
const result2 = applyColor(data, colors2);

console.log(result1);
console.log(result2);


推荐阅读