Mojo
Mojo

Reputation: 518

how do i stop duplicate data entry in array of object

I have an array with name validationValueIdValues so when user clicks on the input state ie. validationIdValue takes the value of individual input box. Now one user select on of option that is either Yes or No. The function creates an obj with key name equal to state, validationIdValue and value equal to users selection in short creates an obj like this

{08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b: "NO"}

Then this object is pushed to validationValueIdValues variable. Now problem arises when user select same box and changes select value from NO to YES

  {08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b: "YES"}

Then this object is pushed to the validationValueIdValues variable without checking if previous same obj with key exist or not and if exist then delete that obj from array. I am using following code for this purpose but some how duplicate obj still exist in an array.

  var validationValueIdValues = []  //as global variable

  if (this.state.selectedOption === "YES") {
    const attribute = {};
    attribute[this.state.validationIdValue] = "YES";
    if (validationValueIdValues.length > 0) {
      validationValueIdValues.forEach((data, index) => {
        if (Object.keys(data) == this.state.validationIdValue) {
          validationValueIdValues.splice(index, 1);
            validationValueIdValues.push(attribute);
        }
      });
    } else {
      validationValueIdValues.push(attribute);
    }
  }

I have used same if statement for this.state.selectedOption === "NO"

Upvotes: 0

Views: 98

Answers (5)

sathish1409
sathish1409

Reputation: 275

You can try this!

const array=[{'08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "YES"},{'08754d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "YES"}];
let newValue = {'08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "YES"};

const index = array.findIndex(e=>Object.keys(e).indexOf(Object.keys(newValue)[0])>-1);
if(index > -1){
    array.splice(index,1,newValue); //Replacing the value
} else {
    array.push(newValue);
}

Upvotes: 1

kooskoos
kooskoos

Reputation: 4859

const newObject = {'08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "NO"}

const array = [{'08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "YES"}, {'tyhgsgsvbb': 'NO'}];

const items = array.filter(item => item['08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b'])

if(allKeys.length>0) array[array.findIndex(items[0])] =  newObject
else array[array.length] = newObject

Upvotes: 1

Junius L
Junius L

Reputation: 16122

Use .findIndex() with .some() to see if the value exists in the array, if exists update the value or push the new value.

const array = [{'08765d0f-2e7a-4fbd-9282-c38f5ddf8d8b': "YES"}, {'tyhgsgsvbb': 'NO'}];

const found = array.findIndex((item) => Object.keys(item).some(key => key === 'tyhgsgsvbb'));

if (found) {
  // toggle the value from yes to no, or vice versa
  array[found] = array[found] === 'YES' ? 'NO' : 'YES'
} else {
  array.push({newKey: 'newValue'});
}

console.log(array)

Upvotes: 1

ssbarbee
ssbarbee

Reputation: 1712

You will need to check all keys in the array to find the matching value. You don't need the code to be repeated for "YES" or "NO" as you mentioned. Something like this is generic enough to handle it

const validationValueIdValues = []  //as global variable


const addOrUpdateValidationValues = (newValidationValue) => {
    if(!newValidationValue) {
        return;
    }
    const keyOfNewValue = Object.keys(newValidationValue)[0]
    let added = false
    // loop through all values
    for(const validationValue of validationValueIdValues) {
        const keyOfCurrentValue = Object.keys(validationValue)[0]
        if(keyOfNewValue === keyOfCurrentValue) {
            // found that kind of key then just replace the value 
            added = true
            validationValue[keyOfCurrentValue] = newValidationValue[keyOfNewValue]
        }
    }
    // not added push
    if(!added) {
        validationValueIdValues.push(newValidationValue)
    }

}


// ...
// when you have the select or what ever event
// just call
addOrUpdateValidationValues(this.state.selectedOption)

Upvotes: 1

kr153
kr153

Reputation: 282

'in' can be used to check for the key in array

// if key not in array then add to array
if ( !( 'mydata' in myArray) ) {
    myArray['mydata'] = 99;
}

Upvotes: 0

Related Questions