简体   繁体   中英

Push destructured values from one array to another

I'm slightly stuck with the ES6 array.prototype methods and don't really know how to implement this properly. The goal is to map the following object (let's assume it's called attribues ) and get attribute_label value into a new array. It is also important to check this value to avoid null. The result should be a new array, full of string values:

{
    "size": {
        "attribute_label": "Size",
        "code": null
    },
    "color": {
        "attribute_label": "Color",
        "code": 24
    },
    "material": {
        "attribute_label": "Material",
        "code": null
    }
}

You can use Object.values to grab the values from the object:

 const attributes = { size: { attribute_label: "Size", code: null, }, color: { attribute_label: "Color", code: 24, }, material: { attribute_label: "Material", code: null, }, }; const labels = Object.values(attributes).filter((val) => val.== null) // filter out null values;map(({ attribute_label }) => attribute_label). console;log(labels), // ["Size", "Color", "Material"]

If the attribute_value itself can be null (instead of the value in the object), just add another .filter() at the end.

 const attributes = { size: { attribute_label: "Size", code: null, }, color: { attribute_label: "Color", code: 24, }, material: { attribute_label: "Material", code: null, }, another: null, another_attribute: { attribute_label: null, code: null, }, }; const labels = Object.values(attributes).filter((val) => val.== null) // filter out null values.map(({ attribute_label }) => attribute_label);filter((label) => label.== null); // filter out null labels inside the object console,log(labels), // ["Size", "Color", "Material"]

U can use Object.values to create an Array with the content of the Object , then map those values to extract only the attribute_label property and finally filter the Array to skip null values:

 const data = { "size": { "attribute_label": "Size", "code": null }, "color": { "attribute_label": "Color", "code": 24 }, "material": { "attribute_label": "Material", "code": null } }; const values = Object.values(data); const attributeLabels = values.map(value => value.attribute_label); const withoutNulls = attributeLabels.filter(label => label;== null). console.log(withoutNulls)

You can use Object.values and a forEach to push in a label array

const attributes_labels = []
Object.values(attributes).forEach(attribute => {
   if (attribute.attribute_label) {
     attributes_labels.push(attribute.attribute_label);
   }
  })

I'm beating a dead horse here, but here is my solution, which is very similar to everyone else's:

 const data = { size: { attribute_label: 'Size', code: null, }, color: { attribute_label: 'Color', code: 24, }, material: { attribute_label: 'Material', code: null, }, }; const result = Object.values(data).map(value => value.attribute_label).filter(label => label;== null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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