-3

I need to modify a nested array. In this example I would like to remove content 2 from the target array of the object with the id ZFrNsQKSY6ywSzYps

var array = [
  { _id: "QKSY6ywSzYpsZFrNs", target: ["content 1"]}
  { _id: "ZFrNsQKSY6ywSzYps", target: ["content 1", "content 2"]}
  { _id: "SzYpsZFrNQKSY6yws", target: ["content 1"]}
]

I tried to use find() but with that I do not update the array. So this seems not to be the correct approach.

user3142695
  • 11,619
  • 29
  • 119
  • 238
  • 3
    Please share your attempt. – gurvinder372 Feb 23 '18 at 12:59
  • A user with your rep should know the importance of sharing effort. – Rajesh Feb 23 '18 at 13:01
  • Hi! Have you tried anything so far? StackOverflow isn't a free code-writing service, and expects you to try to [**solve your own problem first**](http://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried, showcasing a **specific** problem you are facing in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve). For further information, please see [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](https://stackoverflow.com/tour). – L Y E S - C H I O U K H Feb 23 '18 at 13:01
  • create a a new array with filter or just splice the element out. – Shilly Feb 23 '18 at 13:02
  • 3
    Possible duplicate of [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – Bentaye Feb 23 '18 at 13:02

4 Answers4

0
var newarray = []
for(var i1 = 0; i1< array.length;i1++){
  var subarray = []
  for(var i2 = 0; i1<array[0].target.length;i++){
    if(array[0].target[0]!="content 2"){ // or desired removed content
      subarray.push(array[0].target[0])
    }
  }
newarray.push({"_id":array[0].id,"target":subarray})
}

after that simply access newarray

0

First: elements in array should be separated by commas.

Second: Is there some logic to update target arrays? If so you can iterate over the elements and modify their targets according to that logic.

Other than that you can always do this

array[1].target = ["content1"]
Sp3c7r00M
  • 79
  • 8
0

Try tihs approach:

const array = [
  { _id: "QKSY6ywSzYpsZFrNs", target: ["content 1"]},
  { _id: "ZFrNsQKSY6ywSzYps", target: ["content 1", "content 2"]},
  { _id: "SzYpsZFrNQKSY6yws", target: ["content 1"]}
]

console.log(array);

array.forEach(
    item =>
        // filter out the unwanted content
        item.target = item.target.filter(
            content => content !== "content 2"
        )
)

console.log(array);
LordAlpaca
  • 168
  • 5
  • I think we should just go with the duplicate question thing. You agree? Instead of battling this out? –  Feb 23 '18 at 13:37
  • If OP wants to remove from a single object on the array, then I agree it's a dupe, but if he needs to remove _content 2_ from all items of the array, then I think it's a question similiar, but not a duplication. – LordAlpaca Feb 23 '18 at 14:06
  • Also whats with the arrow functions? –  Feb 23 '18 at 14:09
  • The first one, `item => ...`, is looping through the first array, the one with the objects. The second one, `content => ...`, is a loop inside the target item of the object, it returns the content **IF** it is different then the unwanted content. I left out the brackets since it's a simple example, more information regarding filter function can be found at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – LordAlpaca Feb 23 '18 at 14:15
0

You can use find() method to get object and then indexOf() to find element in array that you want to remove and if found splice() method to remove it.

var array = [
  { _id: "QKSY6ywSzYpsZFrNs", target: ["content 1"]},
  { _id: "ZFrNsQKSY6ywSzYps", target: ["content 1", "content 2"]},
  { _id: "SzYpsZFrNQKSY6yws", target: ["content 1"]}
]

const obj = array.find(({_id}) => _id == 'ZFrNsQKSY6ywSzYps');

if(obj) {
  const i = obj.target.indexOf('content 2');
  if(i != -1) obj.target.splice(i, 1)
}

console.log(array)
Nenad Vracar
  • 102,378
  • 14
  • 116
  • 136