简体   繁体   English

嵌套对象和数组解构

[英]Nested object and array destructuring

I am trying to convert an object to a leaner version using destructuring. 我试图使用解构将对象转换为更精简的版本。

My object includes a nested array which also contains objects, from this array I would only like a few fields. 我的对象包括一个嵌套数组,它也包含对象,从这个数组我只想要几个字段。

I can do the nested object destructuring fine, and the array destructuring fine but not together? 我可以做嵌套对象解构很好,并且数组解构很好但不在一起吗?

My current try looks like this: 我目前的尝试看起来像这样:

var data = {
    title: "title1",
    bar: "asdf",
    innerData: [
       {
        title: "inner-title1",
        foo: "asdf"
       },
       {
        title: "inner-title2",
        foo: "asdf"
       }
    ]
};

var { title, innerData: [ { title} ] } = data;

console.log(title);

for (var { title} of innerData) {
  console.log(title);
}

But get a message saying innerData is not defined. 但是得到一条消息说innerData is not defined.

The outcome I would like might be: 我希望的结果可能是:

{
    title: "title1",
    innerData: [
       {
        title: "inner-title1"
       },
       {
        title: "inner-title2"
       }
    ]
};

Your destructuring doesn't do what you think. 你的解构不符合你的想法。

var { title, innerData: [ { title} ] } = data;

is (essentially) equivalent to 是(基本上)相当于

var title = data.title;
var title = data.innerData[0].title;

Destructuring pulls out individual values, it will not map through the array for you. 解构会提取单个值,它不会为您映射数组。 You'll need to do that manually if that is what you want. 如果这是您想要的,您需要手动执行此操作。

You can adjust the variable name to an identifier other than defined innerData ; 您可以将变量名称调整为除定义的innerData之外的标识符; use .map() or JSON.stringify() , JSON.parse() to filter title property from innerData objects 使用.map()JSON.stringify()JSON.parse()innerData对象中过滤title属性

var {title, titles = data.innerData.map(o => ({title:o.title}))} = data;

to maintain innerData variable name you can use array destructuring of object 要维护innerData变量名,可以使用对象的数组解构

var [title, innerData] = [data.title, data.innerData.map(o => ({title:o.title}))];

using JSON.stringify() , JSON.parse() 使用JSON.stringify()JSON.parse()

var [title, innerData] = JSON.parse(JSON.stringify([data.title, data.innerData], ["title"]));

Edit 编辑

If requirement is to create an array containing all title properties within data you can use JSON.stringify() with replacer array set to ["title"] , JSON.parse() , spread element 如果要求创建一个包含data中所有title属性的数组,可以使用JSON.stringify() ,将replacer数组设置为["title"]JSON.parse() ,spread元素

 var data = { title: "title1", bar: "asdf", innerData: [ { title: "inner-title1", foo: "asdf" }, { title: "inner-title2", foo: "asdf" } ] }; var innerData = JSON.parse(JSON.stringify([data, ...data.innerData], ["title"])) console.log(innerData); 

As per @loganfsmyth suggested, I will do it in two steps. 根据@loganfsmyth的建议,我将分两步完成。

var { title } = data;

for (var { title } of data.innerData) {
  console.log( title );
}

Edit: my final solution to build a new json object from the old 编辑:我从旧版本构建一个新的json对象的最终解决方案

const request = { innerData: [] };

({ title } = this.data);

for (const { title} of this.data.innerData) {
    request.innerData.push({
        title
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM