0

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
    }
}
2
  • What methods have you tried in order to achieve this? Commented Mar 23, 2021 at 17:26
  • 'It is also important to check this value to avoid null' ... you mean check attribute_label is not null or the code property is not null as your example seems to suggest? Commented Mar 23, 2021 at 17:42

4 Answers 4

1

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"]

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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);
   }
  })

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.