How to update object except current index in Angular 8

Viewed 60
this.StaticData = {
  "values": [
    {
      "value": "test",
      "label": "test"
    },
    {
      "value": "aa",
      "label": "bb"
    },
    {
      "value": "cc",
      "label": "dd"
    }
  ]
};

I have above object of data. I wanted to return all object except currentIndex.

For example -

suppose in above objects, if I am going to edit 0th index values, and I have updated "value": "rest", instead of "value": "test" and "label": "test" need to keep as it is. So in that case, it will allow to update the values.

{
    "value": "rest",
    "label": "test"
},

But if I tried to enter "label": "bb" and "label": "dd", so it will return false, because these values are already available in above objects.

isLabelExist() {
    const formData = this.editStaticParametersForm.value;
    const currentIndex: number = this.StaticData.values.indexOf(this.selectedRowValue);
    if (formData.label_value && this.StaticData) {
      var isPresent = this.StaticData.values.some(function (el) {
        return el.label === formData.label_value
      });
      if (isPresent) {
        return false;
      }
    }
    return true;
  }
1 Answers

using find (or some) you can check the "index" (add a second argument to the function find), so,

var isPresent = this.StaticData.values.some(function (el,i) {
        return el.label === formData.label_value && i!=currentIndex
      });

Really in .ts we use arrow flat and use const or let, not var

const isPresent = this.StaticData.values.some((el,i)=> {
        return el.label === formData.label_value && i!=currentIndex
      });

Or

const isPresent = this.StaticData.values.some(
    (el,i)=> el.label === formData.label_value && i!=currentIndex);
Related