Home > Net >  Change value or remove object from array of objects
Change value or remove object from array of objects

Time:09-02

I've an array of objects and I need to change the values or "decorate" array:

  1. If value in some object is empty, remove whole object from the array.
  2. If value is an object, take only label or name

My array:

[
{ "name": "certificate", "value": "", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Certificate", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": [ "2017" ], "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "key": "g", "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "packaging", "value": " ", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Packaging", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "key": "gold", "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]

expected output:

[
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": "2017", "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]

any idea how to handle it?

CodePudding user response:

One of the approaches could be

let array = [
    { "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
    { "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
    { "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
    { "name": "yearsOfIssue", "value": "2017", "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
    { "name": "fineWeightUnit", "value": { "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
    { "name": "preciousMetal", "value": { "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]

let newArr = array.map((item) => {
    if (!item.value) return
    if (typeof item === "object" && Object.keys(item).length > 0) {
        if (item["key"]) {
            delete item["key"]
        }
        return item
    } else return item

});

console.log(newArr)

CodePudding user response:

My approach to handle this issue will be:

const arr = [{
    "name": "certificate",
    "value": "",
    "attributeDefinition": {
      "type": {
        "name": "text",
        "__typename": "TextAttributeDefinitionType"
      },
      "label": "Certificate",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "manufacturer",
    "value": "China Mint",
    "attributeDefinition": {
      "type": {
        "name": "text",
        "__typename": "TextAttributeDefinitionType"
      },
      "label": "Manufacturer",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "countryOfOrigin",
    "value": "China",
    "attributeDefinition": {
      "type": {
        "name": "text",
        "__typename": "TextAttributeDefinitionType"
      },
      "label": "Country of origin",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "grossWeight",
    "value": 30,
    "attributeDefinition": {
      "type": {
        "name": "number",
        "__typename": "NumberAttributeDefinitionType"
      },
      "label": "Gross weight",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "yearsOfIssue",
    "value": ["2017"],
    "attributeDefinition": {
      "type": {
        "name": "set",
        "__typename": "SetAttributeDefinitionType"
      },
      "label": "Year(s) of issue",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "fineWeightUnit",
    "value": {
      "key": "g",
      "label": "g"
    },
    "attributeDefinition": {
      "type": {
        "name": "enum",
        "__typename": "EnumAttributeDefinitionType"
      },
      "label": "Fine weight unit",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "packaging",
    "value": " ",
    "attributeDefinition": {
      "type": {
        "name": "text",
        "__typename": "TextAttributeDefinitionType"
      },
      "label": "Packaging",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
  {
    "name": "preciousMetal",
    "value": {
      "key": "gold",
      "label": "Gold"
    },
    "attributeDefinition": {
      "type": {
        "name": "enum",
        "__typename": "EnumAttributeDefinitionType"
      },
      "label": "Precious metal",
      "__typename": "AttributeDefinition"
    },
    "__typename": "RawProductAttribute"
  },
];

arr.forEach((objects, index) => {
  for (let object in objects) {
    if (typeof objects[object] !== 'object') {
      let value = objects[object].toString().trim();
      if (!value.length) {
        arr.splice(index, 1);
      }
    } else {
      if (!objects[object].toString().trim().length) {
        arr.splice(index, 1);
      }
    }
  }
})

CodePudding user response:

Here is a proposal, you can filter to remove empty values and make a map with a condition on type of the value property.

const data = [
{ "name": "certificate", "value": "", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Certificate", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": [ "2017" ], "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "key": "g", "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "packaging", "value": " ", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Packaging", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "key": "gold", "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]

const arrNoEmptyValues = data.filter( element => element.value && element.value != " ")

const result = arrNoEmptyValues.map( element => {
  if(typeof element.value === "object") {
    return {
        ...element, 
        value:{ label: element.value.label || "", name: element.value.name || "" } 
        }
  }
  else {
    return element
  }
})

//there is a case you didn't forecast it's when there is no name nor label in value, i propose to return empty "" label and name in this case, so your object structure when value is an object is more consistent
console.log(result)

  • Related