0

I would like to ask how to remove an element inside an array if the value of object matches with the condition.

For example:

[
    {
        "id": 1, 
        "sender": "a", 
        "receiver": "b"
        }, 
    {
        "id": 2, 
        "sender": "b", 
        "receiver": "c"
    }
    {
        "id": 3, 
        "sender": "a", 
        "receiver": "c"
    }
]

I want to remove the element if sender and/or receiver has a value of "a", which in this case, ids 1 and 3. How do I do that? I have read about splice and I tried to for loop but was not able to accomplish what I wanted.

This was what I tried:

  for (let index = 0; index < arr.length; index++) {
    if (arr[index].sender == "a") {
      arr.splice(index.sender, 1);
    }
    if(arr[index].receiver == "a") {
      arr.splice(index.receiver, 1);
    }
  }

Still, object in array was not removed.

rod james
  • 369
  • 3
  • 13
  • Yes, `arr.splice(index, 1);` will probably work – Alon Eitan Jun 08 '21 at 04:49
  • yes I tried but results of arr is still the same. – rod james Jun 08 '21 at 04:51
  • If you `splice` the array you are currently looping, it will break the loop because the indexes won't be the same after splice. You need to loop in the reverse direction. Check the duplicate – adiga Jun 08 '21 at 04:55
  • 1
    arr = arr.filter(item => item.sender !== 'a' && item.receiver !== 'b'); – Ravi Kiran Jun 08 '21 at 04:58
  • 1
    `arr.reduce((a, c)=>{if(c.sender !== 'a' && c.receiver !== 'a') a.push(c); return a;},[]);` – Steve Jun 08 '21 at 05:09
  • @adiga Why would you close this post and link to a post 9 years old. There are much better ways to do this now. – Steve Jun 08 '21 at 05:17
  • @Steve It is a duplicate with 19 answers and it has [91 linked questions](https://stackoverflow.com/questions/linked/9882284). If have an answer that isn't mentioned in the duplicate, you can add it there. What better ways are you referring to? The duplicates also have answers which have `filter`. – adiga Jun 08 '21 at 05:44
  • @adiga Honestly, that post is a bit `tldr;` As a developer, I really don't have the time to sift through 19 different answers and 91 linked questions to find an answer. I look for the green checkmark and if it doesn't work for my situation, I move on. It's kind of like clicking on the next page button in a Google search. The better method I would use is above my other comment, using `Array.reduce()`. And there is nothing on that post that I could see about reduce. – Steve Jun 08 '21 at 05:58
  • @Steve OP wants to mutate the array. The accepted answers help with that. `filter` and `reduce` create new arrays. Not sure how `reduce` will be different than `filter`. If you have solution with `reduce`, you can post that in the duplicate and link it back here. If you want to make it specific to this question, you can also add a fiddle. OP's issue is splicing in for loop. The duplicate and 91 linked questions address that. If you check only the accepted answer, that is not my fault. – adiga Jun 08 '21 at 06:08
  • @adiga I guess I just don't understand why everyone is so quick to close posts and mark them as duplicate. True it's not your fault I don't have all day to sit on this site and read every last line of a post. I like to help out others. So, what does it really hurt if I can post an answer to help a person to their individual question? Why do we make people jump through so many hoops by having to link to 91 related posts, when instead there are people that are willing to address individual needs and don't get all bent out of shape of there is some quasi-related post from 9 years ago? – Steve Jun 08 '21 at 06:20
  • @Steve This is a duplicate. If you loop from the reverse direction and splice, it works. Your and Ravi Kiran's comments are helpful for OP. If you have an issue with duplicate, you can discuss it on [meta] – adiga Jun 08 '21 at 06:22
  • @adiga And yes, you can mutate an array with reduce. I was doing it in a snippet before you axed the post. As a true developer you need to be able to think outside of the box and show people other options. Looping through an array is not always the best option. But I better let you get back to closing out posts and marking them as duplicates. Because I'm pretty sure that most questions have a quasi-related post somewhere. This is why I have never asked a question on this site. People make you feel like total crap for even asking a question. – Steve Jun 08 '21 at 06:30
  • 1
    Hey guys. Thank you for the opinion that you have presented. Actually both have valid points. Though for me, I just hope the person who answers the questions before closing would ask the OP first if it relates or answers the question since there have been a lot of times that someone closes my question however, the given answer did not solve the problem or I was unable to understand the provided answer and still need further explanation. and I cant unclose my own question. – rod james Jun 08 '21 at 06:31
  • Here's a fiddle: https://jsfiddle.net/t7caxe0b/ – adiga Jun 08 '21 at 07:24

1 Answers1

1

You could do this easier with the filter method. You use it like this:

   let array = [
  {
    id: 1,
    sender: 'a',
    receiver: 'b'
  },
  {
    id: 2,
    sender: 'b',
    receiver: 'c'
  },
  {
    id: 3,
    sender: 'a',
    receiver: 'c'
  }
];

// filter with one condition i.e either sender or receiver
let modifiedArray = array.filter(function(item) {
  return item.sender !== 'b';
});
console.log(modifiedArray);

// Filter with both conditions i.e sender and receiver is checked
let mod_array = array.filter(function(item) {
  if (item.sender !== 'a' && item.receiver !== 'a') {
    return item;
  }
});

console.log(mod_array);

    
RGA
  • 303
  • 3
  • 11
  • Hello thank you for taking time to answer my question. I tried your answer like this: `mod_arr.filter(function(item){ return item.receiver !== "a";});` But element was not removed. – rod james Jun 08 '21 at 05:10
  • I have edited. Now modifiedArray has the filtered data. – RGA Jun 08 '21 at 05:19
  • You can check in this https://stackblitz.com/edit/js-n6czff?file=index.js – RGA Jun 08 '21 at 05:29
  • Thank you. I forgot to assign the array.filter to a variable. hahah Noob mistake. – rod james Jun 08 '21 at 06:33