简体   繁体   English

从另一个数组替换数组中的对象

[英]replace objects in array from another array

I'm trying to replace object(s) in an array from another array while keeping the index/order in place.我正在尝试从另一个数组替换数组中的对象,同时保持索引/顺序到位。

So arrayOne:所以arrayOne:

[
    {
        "color": "#f8edd1",
        "selected": true
    },
    {
        "color": "#d88a8a",
        "selected": false
    },
    {
        "color": "#474843",
        "selected": false
    },
    {
        "color": "#9d9d93",
        "selected": true
    },
    {
        "color": "#c5cfc6",
        "selected": false
    }
]

arrayTwo:数组二:

[
    "#1c2130",
    "#028f76",
    "#b3e099",
    "#ffeaad",
    "#d14334"
]

my desired new array would be:我想要的新数组是:

[
    "#f8edd1",
    "#028f76",
    "#b3e099",
    "#9d9d93",
    "#d14334"
]

So the selected color(s), would still be in the same index/position in the array.因此, selected颜色仍将位于数组中的相同索引/位置。 The desired array can be a completely new array or updated arrayOne or Two.所需的数组可以是全新的数组或更新的数组一或二。 The main issue I ran into was mapping it when there was more than 1 object with the selected: true .我遇到的主要问题是当有超过 1 个 object 且selected: true时映射它。

This was my first stab at it:这是我第一次尝试它:

  const selectedColors = arrayOne.filter(function (obj) {
    return obj.selected === true
  })
  if (selectedColors) {
    const lockedIndex = arrayOne.findIndex((obj) => {
      if (obj.color === selectedColors[0].color) {
        return true
      }
    })
    const newColors = arrayTwo.splice(lockedIndex, 1, selectedColors[0].color)
    console.log(newColors)
  }

Also note that arrayOne is actually a React useState, but i'm not looking to update the useState, I'm doing something else with the newColors - but if it's easier to create a seperate useState to execute what i'm trying to do, that's fine.另请注意,arrayOne 实际上是一个 React useState,但我不想更新 useState,我正在使用newColors做其他事情 - 但如果创建单独的 useState 来执行我正在尝试做的事情更容易,没关系。

You can probably just use .map function on arrayTwo and use second argument which is an index.您可能只在 arrayTwo 上使用.map arrayTwo并使用作为索引的第二个参数。 Then just return original element if corresponding element was not selected or the value of that element:如果未选择相应元素或该元素的值,则只需返回原始元素:

arrayTwo.map((element, index) => arrayOne[index].selected ? arrayOne[index].color : element)

 const arrayOne = [ { "color": "#f8edd1", "selected": true }, { "color": "#d88a8a", "selected": false }, { "color": "#474843", "selected": false }, { "color": "#9d9d93", "selected": true }, { "color": "#c5cfc6", "selected": false } ]; const arrayTwo = [ "#1c2130", "#028f76", "#b3e099", "#ffeaad", "#d14334" ] const result = arrayTwo.map((element, index) => arrayOne[index].selected? arrayOne[index].color: element); console.log(result);

I don't know if you'll need arrayOne and arrayTwo later on so I created a third one like so:我不知道您以后是否需要 arrayOne 和 arrayTwo,所以我创建了第三个,如下所示:

const arrayThree = [];
arrayOne.forEach(function(element, index) {
  arrayThree.push(element.selected ? element.color : arrayTwo[index]);
});

Here, we iterate on arrayOne with a forEach loop.在这里,我们使用 forEach 循环对 arrayOne 进行迭代。 We use two parameters to get corresponding values:我们使用两个参数来获取对应的值:

element = the object at a given index in arrayOne element = arrayOne 中给定索引处的 object

Be aware that forEach loop is asynchronous code.请注意 forEach 循环是异步代码。 So, if you need arrayThree the line right after the forEach loop and you have to iterate a list of hundreds of element, you may get an empty array.因此,如果在 forEach 循环之后需要 arrayThree 行并且必须迭代数百个元素的列表,则可能会得到一个空数组。 In that case, you can try to use a regular loop:在这种情况下,您可以尝试使用常规循环:

const arraythree = [];
for(const [index, element] of arrayOne.entries()) {
  arrayThree.push(element.selected ? element.color : arrayTwo[index]);
}

If you need more information, let me know.如果您需要更多信息,请告诉我。

 const arrayOne = [ { "color": "#f8edd1", "selected": true }, { "color": "#d88a8a", "selected": false }, { "color": "#474843", "selected": false }, { "color": "#9d9d93", "selected": true }, { "color": "#c5cfc6", "selected": false } ]; const arrayTwo = [ "#1c2130", "#028f76", "#b3e099", "#ffeaad", "#d14334" ]; const arrayThree = []; arrayOne.forEach(function(element, index) { arrayThree.push(element.selected? element.color: arrayTwo[index]); }); console.log('result: ', arrayThree)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM