0

my problem is to update an array , containing objects , and each object contains array , i want to update the global array , with values refering to array inside objects , this logic !

generalArray = [{name:String, features:String[]}]

// Try edit message
let array1 = [{ name: "num", features: ['id']  },
            { name: "cat", features: ['gender'] }];
ob = {name:'num2', features:['id']};

function updateArr(arr,ob){

  const index = arr.findIndex(x => 
      ob.features.toString() === x.features.toString()
                              );
    if (index === -1) {
        arr.push(ob);
    } else {
        arr[index] = ob;
    }
}
console.log(array1);
updateArr(array1,ob);
console.log(array1);

this is working perfectly when features array of any object contains one string , but if it contains more than one string , exm features=['id','gender' ] it can't do anything ! help please and thanks

Misiur
  • 5,019
  • 8
  • 38
  • 54
Coder
  • 75
  • 1
  • 8
  • 1
    Please give examples of what you're expecting, it's not very clear. – djcaesar9114 Apr 10 '20 at 12:26
  • i see logic determining if you you should push the object to the array. what is the desired result? Perhaps you can share share a few example inputs and what the outputs should be. – sdotson Apr 10 '20 at 12:29
  • Looks like it works to me. Are you trying to add to one of the `features` arrays or to the `array1` array? – twharmon Apr 10 '20 at 12:29

3 Answers3

1

Here I made a solution to your problem

var array1 = [{ name: "num", features: ['id', 'gender']},
            { name: "cat", features: ['gender']}];
ob = {name:'num2', features:['id']};

function updateArr(arr, ob){
  for(var i = 0;i < arr.length; i++) {
    if(ob.features.join("") === arr[i].features.join("")) {
      arr[i] = ob;
      return;
    }
  }
  arr.push(ob);
}

updateArr(array1, ob);
console.log(array1);
Saadi Toumi Fouad
  • 2,779
  • 2
  • 5
  • 18
1

Option 1: When order of the elements in the features array does not matter.

You can simply change the compare operator in your below line of code

ob.features.toString() === x.features.toString()

to

JSON.stringify(ob.features.sort()) === JSON.stringify(x.features.sort())

Option 2: If the order of the elements in the features array matter. Then you can simply remove .sort().

Note: If you do not want to use stringify, then you can use the array compare function as mentioned in answer here - https://stackoverflow.com/a/16436975/989139.

monish001
  • 671
  • 2
  • 8
  • 20
0

let array1 = [{ name: "num", features: ['id']  },
            { name: "cat", features: ['gender'] }];
ob = {name:'num2', features:['id']};

function updateArr(arr,ob){

  const index = arr.findIndex(x => 
  ob.features.includes(x.features)
     // ob.features.toString() === x.features.toString()
                              );
                              debugger
    if (index === -1) {
        arr.push(ob);
    } else {
        arr[index] = ob;
    }
}
console.log(array1);
updateArr(array1,ob);
console.log(array1);
Jadli
  • 858
  • 1
  • 9
  • 17