javascript 在 es6 中解构对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49413544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:34:27  来源:igfitidea点击:

Destructuring array of objects in es6

javascriptecmascript-6destructuring

提问by ssss

In es6, how can i simplify the following lines using destructuring?:

在 es6 中,如何使用解构来简化以下几行?:

const array0 = someArray[0].data;
const array1 = someArray[1].data;
const array2 = someArray[2].data;

回答by nem035

Whether using destructuring would actually be a simplification is debatable but this is how it can be done:

使用解构实际上是否是一种简化是有争议的,但这是如何做到的:

const [
  { data: array0 },
  { data: array1 },
  { data: array2 }
] = someArray

Live Example:

现场示例:

const someArray = [
  { data: 1 },
  { data: 2 },
  { data: 3 }
];

const [
  { data: array0 },
  { data: array1 },
  { data: array2 }
] = someArray

console.log(array0, array1, array2);

What is happening is that you're first extracting each object from someArraythen destructuring each object by extracting the dataproperty and renaming it:

发生的事情是您首先从中提取每个对象,someArray然后通过提取data属性并重命名它来解构每个对象:

// these 2 destructuring steps
const [ obj1, obj2, obj3 ] = someArray // step 1
const { data: array0 } = obj1 // step 2
const { data: array1 } = obj2 // step 2
const { data: array2 } = obj3 // step 2

// written together give
const [
  { data: array0 },
  { data: array1 },
  { data: array2 }
] = someArray

Maybe combine destructuring with mapping for (potentially) more readable code:

也许将解构与映射结合起来,以获得(可能)更具可读性的代码:

const [array0, array1, array2] = someArray.map(item => item.data)

Live Example:

现场示例:

const someArray = [
  { data: 1 },
  { data: 2 },
  { data: 3 }
];

const [array0, array1, array2] = someArray.map(item => item.data)

console.log(array0, array1, array2);

回答by Bergi

I believe what you actually want is

我相信你真正想要的是

const array = someArray.map(x => x.data)

If you really want three variables (Hint: you shouldn't), you can combine that mapping with destructuring:

如果你真的想要三个变量(提示:你不应该),你可以将该mapping 与解构结合起来:

const [array0, array1, array2] = someArray.map(x => x.data)

回答by anurag sharma

If you want to do with this pure JS then follow this code snippet. It will help you.

如果您想使用这个纯 JS,请遵循此代码片段。它会帮助你。

let myArray = [
    {
        "_id": "1",
        "subdata": [
            {
                "subid": "11",
                "name": "A"
            },
            {
                "subid": "12",
                "name": "B"
            }
        ]
    },
    {
        "_id": "2",
        "subdata": [
            {
                "subid": "12",
                "name": "B"
            },
            {
                "subid": "33",
                "name": "E"
            }
        ]
    }
]


const array = myArray.map(x => x.subdata).flat(1)

const isExist = (key,value, a) => {
  return a.find(item => item[key] == value)
}

let a = array.reduce((acc, curr) => {
  if(!isExist('subid', curr.subid, acc)) {
    acc.push(curr)
  }
  return acc
}, [])

console.log(a)