Javascript 在数组中插入元素
我有一个函数Javascript 在数组中插入元素,javascript,reactjs,Javascript,Reactjs,我有一个函数 checkName(输出){ output.filter((NewData)=>{ 返回此.props.elements.filter((旧数据)=>{ if(NewData.key==OldData.key){ NewData.name=OldData.name, //在这里,我需要添加另一个元素 //需要添加newData.number=OldData.number } 返回新数据 }) }) 返回输出 } 我把这个函数称为: const named = this.check
checkName(输出){
output.filter((NewData)=>{
返回此.props.elements.filter((旧数据)=>{
if(NewData.key==OldData.key){
NewData.name=OldData.name,
//在这里,我需要添加另一个元素
//需要添加newData.number=OldData.number
}
返回新数据
})
})
返回输出
}
我把这个函数称为:
const named = this.checkName(product.rows)
现在,我需要向传递给checkName的产品数组中添加未在产品中定义的值“OldData.Number”到“newData.Number”(因此我需要创建此字段)
例如:
检查名称功能之前的产品
product.rows = [NewData.name]
product.rows = [NewData.name="value of OldData.name", NewData.number="value of OldData.number"]
检查名称功能后的产品
product.rows = [NewData.name]
product.rows = [NewData.name="value of OldData.name", NewData.number="value of OldData.number"]
如何获得此结果?您可以获得旧对象的键和值
const keys = Object.keys(oldObject);
const values = Object.values(oldObject);
// or
const [keys, values] = Object.entries(oldObject);
之后,您将使用oldObject的所有键创建一个循环,并像数组一样插入newObject
keys.forEach( (key, index) => newObject[key] = values[index]);
// or
for (const [key, value] of Object.entries(object1)) {
newObject[key] = value
}
像这样使用地图
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;
}
代码中有两个令人困惑的地方:
- 您正在使用
在过滤器
数组的每个成员中执行操作。然而,过滤器应该用来。。。好的,过滤那个数组,意思是不应该修改它,只返回它的子集。相反,您可能希望使用。然而,考虑到下一个子弹,你可能想使用输出
- 您正在修改传递给
函数的数组。这是令人困惑的,可能会导致难以发现的错误。相反,让你的函数“纯”,这意味着它不应该改变它的输入,而只是从中返回你需要的数据checkName
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 };
}
}
}
用法如下所示:
const named = this.checkName(product.rows)
请注意,
product.rows
数组不会被修改 您对filter()的使用不正确。当您只需要一个循环时,请使用类似于forEach()
或for()
循环的循环方法filter()
返回一个新数组为什么它不正确??您从未使用filter()
的结果就是原因。你只是用它来循环数组,这不是我用这种方法尝试过的过滤器foruse map()和forEach(),但我收到了未定义的输出,因为outputoutput是数组,对吗?是的,这是一个arrayok,我正在尝试,它似乎是我搜索的!!只有一个问题,如果我想在if(OldData)条件下也返回“Document.Sender.number:OldData.number”
,我该怎么办?Document.Sender.number不在newdate中您可以添加希望从OldData
中获取的任何属性,如下所示:返回{…NewData,name:OldData.name,number:OldData.number}代码>我将用一些注释更新代码以使其更清晰