Javascript 使用映射和承诺将属性添加到对象数组

Javascript 使用映射和承诺将属性添加到对象数组,javascript,object,promise,Javascript,Object,Promise,向数组中的每个对象分配一个新属性(通过map中的异步函数获得)不起作用。 这是我的密码: asyncFunction.then(array => { var promises = array.map(obj => { return otherAsyncFunction(obj._id).then(prop => { obj.url = prop; return obj }) }) Promise.a

向数组中的每个对象分配一个新属性(通过map中的异步函数获得)不起作用。 这是我的密码:

asyncFunction.then(array => {
    var promises = array.map(obj => {
      return otherAsyncFunction(obj._id).then(prop => {
        obj.url = prop;
        return obj
      })
    })
    Promise.all(promises).then(function (results) {
      console.log(results)
    })
    res.send(user.friends)
})
console.log(结果)
显示相同的数组


我试图在返回obj之前记录新的obj,它还显示旧的
obj

我终于找到了答案:
数组
是不可变的,我只是用以下内容复制了初始数组:

let b = JSON.parse(JSON.stringify(array));

如果不进一步研究您的代码,就无法准确地说出错误所在。您能否确保
url
obj
中的可写属性,还是
obj
不是冻结对象?在非严格模式下,这些将以静默方式失败。需要添加的一点是:当我在返回之前显示'obj.url'时,它会显示正确的值。您的意思是它是
asyncFunction()。然后(…)