有条件地操纵数组中元素的属性 [英] Conditionally manipulate elements' properties in array

查看:49
本文介绍了有条件地操纵数组中元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java语言的新手,正在尝试通过玩具示例学习一些基本知识。

I'm new to javascript and trying to learn some basic with toy examples.

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

Say I have an array containing data on six people.

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}
];

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

The array lists each person's id, value, and who they're friends with. E.g., Person 1 is friends with person 3, and person 3 is friends with person 5, and so on.

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

Now I want to manipulate who is friends with who based on each person's value. This is the logic I'd like to implement (probably in a for loop):

IF 人的值是最低或数组中第二低的值,
THEN 将数组中最高值的人的ID添加到其朋友

IF person's value is the lowest or the second lowest value in the array, THEN add the id of the person with the highest value in the array to their friends.

因此,在这种情况下,我的期望输出是:

So my desired output in this case is:

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]}
];

我该怎么做?

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

I've done a very basic manipulation of the array below, where I take away the friend of the person with the highest value in the array. But I've gotten confused as I've moved onto this more complicated task.

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);

推荐答案

您可以传递一次源数组以找出最高,最低和第二最低的值(以及相应的 id ),然后在到达末尾时相应地修改源数组:

You may pass your source array once to figure out the highest, the lowest and the second lowest values (along with corresponding id's), then modify your source array accordingly when you reach the end:

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%;}

这篇关于有条件地操纵数组中元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆