【问题标题】:Conditionally manipulate elements' properties in array有条件地操作数组中元素的属性
【发布时间】:2020-08-18 01:18:46
【问题描述】:

我是 javascript 新手,正在尝试通过玩具示例学习一些基础知识。

假设我有一个包含六个人数据的数组。

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": 6},
    {"id": 5, "value": 55, "friends": 1},
    {"id": 6, "value": 33, "friends": 2}
];

该数组列出了每个人的idvalue,以及他们与谁friends。例如,第 1 个人是第 3 个人的朋友,第 3 个人是第 5 个人的朋友,依此类推。

现在我想根据每个人的价值来操纵谁和谁成为朋友。这是我想要实现的逻辑(可能在 for 循环中):

IF人的值是数组中最低的次低的值, THEN 将数组中具有 最高 值的人的 id 添加到他们的朋友。

所以在这种情况下我想要的输出是:

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": [4, 1]},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": 6},
    {"id": 5, "value": 55, "friends": 1},
    {"id": 6, "value": 33, "friends": [2, 1]}
];

我该怎么做?


我已经对下面的数组进行了非常基本的操作,在这里我带走了数组中值最高的人的朋友。但是当我开始从事这项更复杂的任务时,我感到很困惑。

const myArray = [
    {"id": 1, "value": 75, "friends": 3},
    {"id": 2, "value": 40, "friends": 4},
    {"id": 3, "value": 60, "friends": 5},
    {"id": 4, "value": 62, "friends": 6},
    {"id": 5, "value": 55, "friends": 1},
    {"id": 6, "value": 33, "friends": 2}
];

// Finds max and min values in array
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
    tmp = myArray[i].value;
    if (tmp > highest) highest = tmp;
};

for(i = 0; i < myArray.length; i++){
    // If person has the highest value in the array
      if(myArray[i].value == highest){
        // Then take away their friend
        myArray[i].friends = NaN
      } else {
        myArray[i].friends = myArray[i].friends
      }
  };

  console.log(myArray);

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您可以通过一次源数组来计算出最高值、最低值和次低值(以及对应的id),然后在到达末尾时相应地修改源数组:

    const src = [
        {"id": 1, "value": 75, "friends": 3},
        {"id": 2, "value": 40, "friends": 4},
        {"id": 3, "value": 60, "friends": 5},
        {"id": 4, "value": 62, "friends": 6},
        {"id": 5, "value": 55, "friends": 1},
        {"id": 6, "value": 33, "friends": 2}
    ],
        
        populateFriends = input => {
          let highest = {value: -Infinity},
              lowest = {value: Infinity},
              secondLowest = {}
          for({id, value} of input){
              if(value > highest.value){
                highest = {id, value}
              } else if(value < lowest.value){
                secondLowest = {...lowest}
                lowest = {id, value}
              }
          }
          return input.map(o => 
            (o.id == lowest.id || o.id == secondLowest.id) && 
            o.friends != highest.id ? 
            {...o, friends: [o.friends, highest.id]} :
            o)
        }
        
    console.log(populateFriends(src))
    .as-console-wrapper{min-height:100%;}

    【讨论】:

      【解决方案2】:

      你可以这样做

      const myArray = [
          {"id": 1, "value": 75, "friends": 3},
          {"id": 2, "value": 40, "friends": 4},
          {"id": 3, "value": 60, "friends": 5},
          {"id": 4, "value": 62, "friends": 6},
          {"id": 5, "value": 55, "friends": 1},
          {"id": 6, "value": 33, "friends": 2}
      ];
      
      const highestValue = myArray.reduce((acc, rec) => {
        return (acc.value < rec.value) ? acc = rec : acc
      }, myArray[0])
      
      const twoLowestPersons = myArray.sort((a, b) => a.value - b.value).slice(0, 2)
      
      const result = myArray.map(rec => {
        const object = twoLowestPersons.find(el => el.id === rec.id)
        if (typeof object !== 'undefined') {
          rec.friends = [rec.friends, highestValue.id]
          return rec
        } else {
          return rec
        }
      }).sort((a, b) => a.id - b.id)
      
      console.log(JSON.stringify(result))

      【讨论】:

      • 使用.sort() 获得两个最低值是一个糟糕的决定,这可能会降低drastically 的性能。
      猜你喜欢
      • 2012-10-05
      • 2019-08-19
      • 2012-10-09
      • 2017-08-10
      • 2015-04-05
      • 1970-01-01
      • 2018-04-22
      • 2011-02-07
      • 1970-01-01
      相关资源
      最近更新 更多