Home > Back-end >  How can delete the nodes from array objects if array key match
How can delete the nodes from array objects if array key match

Time:03-31

I have array of keys i want to delete the nodes from array of object.

I have tried but not working

if(attr_type === "Blush" || attr_type === "Tie Male"
            || attr_type === "Arm Female Brown"
            || attr_type === "Arm Female Grey"
            || attr_type === "Arm Male Brown"
            || attr_type === "Arm Male Grey"
            || attr_type === "Glasses Or Mask Female"
            || attr_type === "Glasses Or Mask Male"
            || attr_type === "Glasses Or Mask Male For Outfit"
            || attr_type === "Shoes Female"
            || attr_type === "Shoes Male"
            || attr_type === "Boy Shoes"
            || attr_type === "Girl Shoes"
            || attr_type === "Outfit Female Arm Grey"
            || attr_type === "Outfit Female Arm Brown"
            || attr_type === "Pants Female"
            || attr_type === "Pants Male"
        
            ) { 
 
                delete attributes[i];
                  
            }

This is script

[
 'Blush',
 'Tie Male',
 'Arm Female Brown',
 'Outfit Female Arm Grey'
]

This array object

 [
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "Blush",
      "value": "None"
    },
    {
      "trait_type": "Body Male Grey",
      "value": "Male Body Grey Color"
    }
    
]

This is final array

[
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "Body Male Grey",
      "value": "Male Body Grey Color"
    }
    
]

The array may be many keys and array object has many nodes.

Please help

Thanks

CodePudding user response:

i would go this way -

const keyWords = [
        'Blush',
        'Tie Male',
        'Arm Female Brown',
        'Outfit Female Arm Grey'
    ]
    
    const arr = [
        {
            "trait_type": "Background",
            "value": "Yellow"
        },
        {
            "trait_type": "Blush",
            "value": "None"
        },
        {
            "trait_type": "Body Male Grey",
            "value": "Male Body Grey Color"
        }
    
    ]
    
    
    const keyWordsSet = new Set(keyWords);
    
    console.log(arr.filter(obj => !keyWordsSet.has(obj.trait_type)))

CodePudding user response:

How about this?

const array1 = [
 'Blush',
 'Tie Male',
 'Arm Female Brown',
 'Outfit Female Arm Grey'
]

const array2 =  [
  {
    "trait_type": "Background",
    "value": "Yellow"
  },
  {
    "trait_type": "Blush",
    "value": "None"
  },
  {
    "trait_type": "Body Male Grey",
    "value": "Male Body Grey Color"
  }
]

console.log(array2.filter(a2 => !array1.includes(a2.trait_type)))

CodePudding user response:

You can achieve it by using Array.filter() along with Array.includes().

Working Demo :

const blackList = [
 'Blush',
 'Tie Male',
 'Arm Female Brown',
 'Outfit Female Arm Grey'
];

const inputArr = [
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "Blush",
      "value": "None"
    },
    {
      "trait_type": "Body Male Grey",
      "value": "Male Body Grey Color"
    }
    
];

const res = inputArr.filter((obj) => !blackList.includes(obj.trait_type));

console.log(res);

CodePudding user response:

const reflist = [
  'Blush',
  'Tie Male',
  'Arm Female Brown',
  'Outfit Female Arm Grey'
]
const arr = [
  {
    "trait_type": "Background",
    "value": "Yellow"
  },
  {
    "trait_type": "Blush",
    "value": "None"
  },
  {
    "trait_type": "Body Male Grey",
    "value": "Male Body Grey Color"
  }
];
const output = arr.filter(item => reflist.indexOf(item.trait_type) === -1);
console.log(output)

  • Related