简体   繁体   English

在数组中插入元素

[英]Insert element inside array

I have a function我有一个 function

checkName(output) {

  output.filter((NewData) => {
    return this.props.elements.filter((OldData) => {
      if (NewData.key == OldData.key) {
        NewData.name = OldData.name,
          //there i need to add another element 
          // Need to add newData.number = OldData.number
      }
      return NewData
    })
  })
  
  return output
}

and I call this function like:我称之为 function 就像:

const named = this.checkName(product.rows)

Now I need to add to my product's array that I passed to checkName the value "OldData.Number" to "newData.Number" that is not defined in product (so I need to create this field)现在我需要将值“OldData.Number”添加到产品中未定义的“newData.Number”到我传递给 checkName 的产品数组中(因此我需要创建此字段)

For example:例如:

Product before the checkName function核对前产品名称 function

product.rows = [NewData.name]

Product after the checkName function核对后的产品名称 function

product.rows = [NewData.name="value of OldData.name", NewData.number="value of OldData.number"]

How can I obtain this result?我怎样才能得到这个结果?

There are 2 confusing things in your code:您的代码中有两件令人困惑的事情:

  • You are using filter to execute an action in each member of the output array.您正在使用filteroutput数组的每个成员中执行操作。 However, filter should be used to... well, filter that array, meaning that is should not modify it, just return a sub-set of it.但是,过滤器应该用于......好吧,过滤那个数组,这意味着不应该修改它,只返回它的一个子集。 Instead, you might want to use forEach .相反,您可能想要使用forEach However, taking into accound the next bullet, probably you want to use map .但是,考虑到下一个项目符号,您可能想使用map
  • You are modifying the array passed to the checkName function.您正在修改传递给checkName function 的数组。 This is confusing and can lead to hard-to-find bugs.这令人困惑,并可能导致难以发现的错误。 Instead, make your function "pure", meaning that it should not mutate its inputs, instead just return the data you need from it.相反,让你的 function “纯”,这意味着它不应该改变它的输入,而只是从它返回你需要的数据。

I would suggest some implementation like this one:我会建议一些像这样的实现:

checkName(output){
    return output.map((NewData) => {
        // find the old data item corresponding to the current NewData
        const OldData = this.props.elements.find(x => x.key === NewData.key);
    
        if (OldData) {
            // If found, return a clone of the new data with the old data name
        
            // This uses the spread syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
            return {
                ...NewData, // Clone the NewData object
                name: OldData.name, // set the value found in OldData.name in the "name" field of the cloned object
                number: OldData.number, // You can do the same for each field for which you want to replace the value cloned from NewValue
            };
        } else {
            // Otherwise, just return a clone of the NewData
            return { ...NewData };
        }
    }
}

The usage would be like this:用法是这样的:

const named = this.checkName(product.rows)

Be aware that the product.rows array won't be modified!请注意product.rows数组不会被修改!

You can get keys and values of the old object.您可以获取旧 object 的键和值。

const keys = Object.keys(oldObject);
const values = Object.values(oldObject);

// or
const [keys, values] = Object.entries(oldObject);

After, you will create a loop with all keys of oldObject, and insert in newObject like a array.之后,您将使用 oldObject 的所有键创建一个循环,并像数组一样插入 newObject。

keys.forEach( (key, index) => newObject[key] = values[index]);

// or

for (const [key, value] of Object.entries(object1)) {
  newObject[key] = value
}

Use map like this.像这样使用 map。

checkName(output){
return output.map(( NewData) =>{
 this.props.elements.forEach((OldData) => {
 if (NewData.key == OldData.key) {
     NewData.name = OldData.name;
     NewData.number = OldData.number;
   }
})
 return NewData;
})
 // return output;
}

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

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