0

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");
}
5
  • In the final output you want a & d or only d from the context or only a from data? Commented Apr 16, 2021 at 6:51
  • In this.selectedParameterContext, right now two objects, In first object i want to keep a and d. and in second object i want d only, bcz a is not avlbl in second object Commented Apr 16, 2021 at 6:59
  • Need to keep only those object which are avlbl in rows.selected_parameter_value remaining all objects need to remove from this.selectedParameterContext Commented Apr 16, 2021 at 7:02
  • Can you create a Stackblitz example? Commented Apr 16, 2021 at 7:04
  • Hi i have edited my question, plz review again. Commented Apr 16, 2021 at 8:11

1 Answer 1

1

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

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

1 Comment

I have edited my question with example, plz review once again. it will more clear now.

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.