0

I have a JSON string:

var jsn = '{"header-v1":{"archives":{"is_author":"all"}},"header-v4":{"archives":{"is_author":"all"}}}';

This object is constantly updated and I want to remove duplicate values. For example, if it is:

    var jsn = '{"header-v4":{"archives":{"is_author":"all"}}}';

And if the new rule set which should be added will be equal to

"header-v1":{"archives":{"is_author":"all"}}

then I want to remove "header-v4":{"archives":{"is_author":"all"}} from there, because there is a duplicate of {"archives":{"is_author":"all"}}.

Is that even possible with JavaScript?

2 Answers 2

1
var result = [];
$.each(subservices, function (i, e) {
    var matchingItems = $.grep(result, function (item) {
       return item.name === e.name && item.label === e.label;
    });
    if (matchingItems.length === 0){
        result.push(e);
    }
});

//displays result [{"name":"hello","label":"world"},{"name":"abc","label":"xyz"}]
alert(JSON.stringify(result));

JS fiddel http://jsfiddle.net/defujjhp/

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

1 Comment

Nope, even this does not work. Please see my comment on @Manish post.
0

Maybe something like this you can do

var jsn = '{"header-v4":{"archives":{"is_author":"all"}}}';

var jsonObject = JSON.parse(jsn);

var newJsn = '{header-v1":{"archives":{"is_author":"all"}}}';

var newJsonObject = JSON.parse(newJsn);

var matchingKey = [];

Object.keys(newJsonObject).forEach(key => {
    Object.keys(jsonObject).forEach(nkey => {
        if(newJsonObject[key].toString() === jsonObject[nkey].toString()) {
          matchingKey.push(nkey);
        }
    });
});

matchingKey.forEach(mkey => {
    delete jsonObject[mkey];
});

6 Comments

Thanks, but if initial object jsonObject contains multiple objects, your code removes all of them. I just need to remove the duplicate of a new object from initial JSON and then insert the new object on its place. In short, I want to replace old (duplicate) value with a new one. Here is a JSBin example: jsbin.com/gekagavabe/edit?js,console
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
Thanks for the effort but it's still not that what I want. Your script removed everything and just added a new string {"header-v1":{"archives":{"is_author":"all"}}} to JSON. When {"header-v1":{"archives":{"is_author":"all"}}} is added I want to remove only "header-v4":{"archives":{"is_author":"all"}}} from there, because it's a duplicate of a new value. Is it clear now?
Do you want to remove only last Object? because "header-v3":{"archives":{"is_search":"all"}} also duplicate of new value so i removed all the duplicate value from object
No, it's not a duplicate. "is_search" and "is_author" is not the same.
|

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.