saurabh
saurabh

Reputation: 79

How to compare object based on key and value and remove from array

I have below data in const rows.

 const rows = {
      "selected_parameter_value": [
        {
          "parameter_value": "a",
          "label_value": "a"
        },
        {
          "parameter_value": "d",
          "label_value": "d"
        }
      ]
    };

I want to compare each and every object of const rows.selected_parameter_value with parameter_value and label_value from this.selectedParameterContext.records[0].selected_parameter_value.

And delete those objects from this.selectedParameterContext.records[0].selected_parameter_value which are not present in const rows.selected_parameter_value

For example -

In the const rows.selected_parameter_value, only a and d object are present, b and c are not present.

So remove object whose parameter and label values are b and c from this.selectedParameterContext.records[0].selected_parameter_value.

  this.selectedParameterContext = {
          'records': [
            {
              'selected_parameter_value': [{
                'parameter_value': 'a',
                'label_value': 'a'
              },
              {
                'parameter_value': 'b',
                'label_value': 'b',
              }]
            },
            {
              'selected_parameter_value': [{
                'parameter_value': 'c',
                'label_value': 'c'
              },
              {
                'parameter_value': 'd',
                'label_value': 'd',
              }]
            }]
        };


    **Expected Output -**

    this.selectedParameterContext = {
  'records': [
    {
      'selected_parameter_value': [{
        'parameter_value': 'a',
        'label_value': 'a'
      }]
    },
    {
      'selected_parameter_value': [
        {
          'parameter_value': 'd',
          'label_value': 'd',
        }]
    }]
};

I tried below code

deleteContextData(rows ) { 
    const paramArray = rows.selected_parameter_value;
    const newArrayData = this.selectedParameterContext.records[0].selected_parameter_value;
    const removeMatchingData = (paramArray, toCompareWith) => {
      return paramArray.filter(({ label_value }) => !toCompareWith.some(compareObj => compareObj.label_value === label_value));
    }
    console.log(removeMatchingData(paramArray, newArrayData),"newarray");
}

Upvotes: 0

Views: 62

Answers (1)

Nithish
Nithish

Reputation: 6039

You can filter the data/source using Array.filter by comparing each object by matching object with the same key-value present in the comparing/target array and return only the ones which are not matching in the source array.

let data = [{label_value: 'a', parameter_value: 'a'}, {label_value: 'b', parameter_value: 'b'}, {label_value: 'c', parameter_value: 'c'}, {label_value: 'd', parameter_value: 'd'}]
let contextData = [{label_value: 'b', parameter_value: 'b'}, {label_value: 'c', parameter_value: 'c'}];

const removeMatchingData = (data, toCompareWith) => {
  return data.filter(({ label_value }) => !toCompareWith.some(compareObj => compareObj.label_value === label_value));
}

console.log(removeMatchingData(data, contextData));

contextData = [{label_value: 'b', parameter_value: 'b'}, {label_value: 'c', parameter_value: 'c'}, {label_value: 'e', parameter_value: 'e'}];

console.log(removeMatchingData(contextData, data));

Upvotes: 1

Related Questions