在我的第一个函数中,我获取了一些信息并将它们推送到我的对象。
autoArry.push({
id: countTxns,
txnID: txnID,
account: buyersAccount,
data1: '',
data2: '',
data3: ''
});
此时,我没有来自 data1、data2 或 data3 的信息,但我还是在对象中创建了它们。
稍后在我的脚本中,我希望能够在通过其他函数收集这些数据值时填充这些数据值。
每个函数中我将拥有相同的一件事是 txnID。
我想我必须以某种方式映射它,然后以这种方式添加额外的数据。
谁能解释一下我该怎么做?
非常感谢
最佳答案
迭代并检查匹配:
function otherFunction(txnID) {
for (var i=0; i<autoArray.length; i++) {
if (autoArray[i].txnID == txnID) {
autoArray[i].data1 = 'something 1';
autoArray[i].data2 = 'something 2';
autoArray[i].data3 = 'something 3';
}
}
}
关于javascript - 将数据添加到映射常量键/值的现有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18293282/