Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用数组映射代替嵌套for循环_Javascript_Arrays_Dictionary_Vue.js_Reduce - Fatal编程技术网

Javascript 如何使用数组映射代替嵌套for循环

Javascript 如何使用数组映射代替嵌套for循环,javascript,arrays,dictionary,vue.js,reduce,Javascript,Arrays,Dictionary,Vue.js,Reduce,在我的场景中,我需要将元素推送到包含对象的地址数组中。我正在使用vue.js 我目前的工作职能是: propagateCustomerInfo(selectedOption, id){ // Propagate addresses this.addresses = selectedOption.addresses // Propagate contact's addresses for (var i = selectedOpti

在我的场景中,我需要将元素推送到包含对象的地址数组中。我正在使用vue.js

我目前的工作职能是:

propagateCustomerInfo(selectedOption, id){

        // Propagate addresses
        this.addresses = selectedOption.addresses

        // Propagate contact's addresses
        for (var i = selectedOption.contacts.length - 1; i >= 0; i--) {
            for (var j = selectedOption.contacts[i].addresses.length - 1; j >= 0; j--) {
                let address = selectedOption.contacts[i].addresses[j]
                address.contact = selectedOption.contacts[i]
                this.addresses.push(address)
            }
        }
    },
selectedOption
对象具有以下结构:

{
   addresses: [
      {
         id: 0,
         street: 'My street'
      },
      {...}
   ],
   contacts: [
      {
         id: 0,
         name: 'Lorem Ipsum',
         addresses: [
            {
               id: 0,
               street: 'My street'
            },
            {...}
         ],
      }
   ]
}
除了将每个联系人的地址对象推送到this.addresses数组之外,我还需要将联系人附加到地址本身,以便进行多选渲染。这就是为什么我要做
address.contact=selectedOption.contacts[I]

我几乎可以肯定,这可以通过一些映射/减少组合以最漂亮的方式实现,但我不知道如何实现

任何帮助都将不胜感激。
谢谢

如果要将联系人变量中的所有地址合并到地址变量中:

    this.contacts.map(contact => this.addresses.push(...contact.addresses))
编辑
要插入contact.id和contact.name,请执行以下操作:

this.contacts.map(contact => {
  let temp = []
    contact.addresses.map(address => {
    temp.push({
      name: contact.name,
      id: contact.id,
      ...address
    })
  })
  this.addresses.push(...temp)
})

使用
address.contact=
,您正在对现有的
address
对象进行变异-您确定这就是您想要的吗?我没有意识到这一点。无论如何,这并不影响我需要什么。谢谢你的警告!好了,差不多完成了。联系人的地址将根据需要推送到地址数组中,但我还需要将联系人附加到其中的每个地址。可能吗?