2

I have the following Javascript object

[  
   "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}",
   "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}"
]

how do I remove the element where ID = "a6f72972-502e-452b-9773-51699a527122"? (doesnt have to literally be "a6f72972-502e-452b-9773-51699a527122", thats just an example.

I have tried the following

var index = detailsArray.map(function (element) {
                    console.log("element = " + JSON.stringify(element) + " index = " + index + " id = " + element.id);
                    return element.id;
                }).indexOf(detailId);
                console.log("index of " + detailId + " = " + index);
                delete detailsArray[index];

But it is returning element.id as undefined. I suspect its because the 'property' of the element is a String, i'm unsure how to solve this.

5
  • 1
    Is there a reason you have an array of strings that look like an object instead of an array of actual objects? Commented Dec 3, 2015 at 3:57
  • 2
    Use filter(). Code: var newArr = arr.filter(function(e) { return JSON.parse(e).id !== uglyId; }); Commented Dec 3, 2015 at 3:58
  • @NickZ Yes, the array is actually from a Java Map<String,String> and it is getting stored this way on my backend. Commented Dec 3, 2015 at 3:58
  • @Tushar I have attempted that before, but my issue is element.id is returning 'undefined'. Thanks for your comment though Edit: I will try your edited comment, thanks. Commented Dec 3, 2015 at 4:02
  • @Tushar Sorry man, i read the old one that didnt have the JSON.parse(e) in it. Commented Dec 3, 2015 at 4:13

2 Answers 2

3

It is just an array of JSON strings.
If you want to filter them, then simply parse every item and check id for equality:

var arr = [  
   "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}",
   "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}"
];

var result = arr.filter(function(x) {
  return JSON.parse(x).id !== 'a6f72972-502e-452b-9773-51699a527122';
});

Here is the working JSFiddle demo.

Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you need to parse those strings as JSON. In the interest of providing a solution that actually mutates the detailsArray by removing the offending index (as opposed to creating a copy without it), here's a sort of indexOf via callback using Array.prototype.reduce

var index = detailsArray.reduce(function(prev, curr, idx) {
    return prev === -1 && JSON.parse(curr).id === detailId ?
        idx : prev;
}, -1);
if (index > -1) {
    detailsArray.splice(index, 1);
}

var detailsArray = [
      "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}",
      "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}"
   ],
   detailId = 'a6f72972-502e-452b-9773-51699a527122';
    
var index = detailsArray.reduce(function(prev, curr, idx) {
  return prev === -1 && JSON.parse(curr).id === detailId ?
    idx : prev;
}, -1);
if (index > -1) {
    detailsArray.splice(index, 1);
}

document.getElementById('out').innerHTML += JSON.stringify(detailsArray, null, '  ');
<pre id="out">detailsArray = </pre>

5 Comments

Thanks for the answer Phil, it is working! One question though since you mentioned the mutate part.. Is there a way to use the JSON.parse without mutating the original array?
@hitch.united by "mutate" I mean that detailsArray is itself modified by removing the desired index as opposed to creating a copy without the found index as seen in the other answers using filter()
Ok I understand, Im running into an issue now when I use JSON.parse(element). It looks like it's actually changing the object in my detailsArray to JSON now, so when it gets saved its breaking my map in java.
@hitch.united you must be altering it somewhere else then. Neither this code nor the code in the other answer changes the array values
You were right, i was changing it somewhere else. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.