-1

I have one nested array. I need to find and remove from inner array values.

mainArray = [
        {
            name:"name1",
            idArray:["1", "2", "3"]
        }
        {
            name:"name1",
            idArray:["1", "2", "3", "5", "2"]
        }
        {
            name:"name1",
            idArray:["1", "2", "3", "4"]
        }
    ]

I need a answer like below:

mainArray = [
        {
            name:"name1",
            idArray:["1", "3"]
        }
        {
            name:"name1",
            idArray:["1", "3", "5"]
        }
        {
            name:"name1",
            idArray:["1", "3", "4"]
        }
    ]

That mean I want remove values from idArray which is id as "2". Can you give me a answer from lodas, it's more comfort.

vijayaganth
  • 147
  • 2
  • 12
  • 4
    Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Apr 10 '18 at 12:29

4 Answers4

1

You can map to loop thru the array. Clone each element and use filter the idArray

let mainArray = [{
    name: "name1",
    idArray: ["1", "2", "3"]
  },
  {
    name: "name1",
    idArray: ["1", "2", "3", "5", "2"]
  },
  {
    name: "name1",
    idArray: ["1", "2", "3", "4"]
  }
];

let toRemove = '2';

let result = mainArray.map(o => {
  o = Object.assign({}, o); //To clone the obj. Will not affect the original array
  o.idArray = o.idArray.filter(v => v !== toRemove);
  return o;
});

console.log(result);

Add reduce to remove the empty idArray

let mainArray = [{
    name: "name1",
    idArray: ["1", "2", "3"]
  },
  {
    name: "name1",
    idArray: ["1", "2", "3", "5", "2"]
  },
  {
    name: "name-removed",
    idArray: ["2"]
  },
  {
    name: "name1",
    idArray: ["1", "2", "3", "4"]
  },
  {
    name: "name-removed",
    idArray: []
  }
];

let toRemove = '2';

let result = mainArray.reduce((c, o) => {
  o = Object.assign({}, o);
  o.idArray = o.idArray.filter(v => v !== toRemove);
  if (o.idArray.length) c.push(o);
  return c;
}, []);

console.log(result);
Eddie
  • 25,279
  • 6
  • 26
  • 53
0

Using lodash could be complex to understand the logic behind the operation. This can be achieved using simple JavaScript as below:

var removeVal = '2';
var mainArray = [
    {
        name:"name1",
        idArray:["1", "2", "3"]
    },
    {
        name:"name1",
        idArray:["1", "2", "3", "5", "2"]
    },
    {
        name:"name1",
        idArray:["1", "2", "3", "4"]
    }
];

mainArray.forEach(function(obj){
  var index = obj.idArray.indexOf(removeVal);
  if(index !== -1 ){
   obj.idArray.splice(index, 1);
  }
});

console.log(mainArray);
Ankit Agarwal
  • 28,439
  • 5
  • 29
  • 55
0

Check out this link: remove an element of an array and resize it They use splice to remove an element an array and resize. Try using this with a multidimensional array. This how I think it should work for your problem mainArray[0].splice(2,3).

Raph
  • 1
0

Iterate the array mainArray and find the unique values from idArray array

mainArray.forEach(function(data){
  data.idArray = findUnique(data.idArray)
});
console.log(mainArray);

function findUnique(a){
return a.filter(function(value){ return a.filter(function(val){ return val === value}).length === 1});
}
Ininiv
  • 1,215
  • 7
  • 12