Delete duplicate object(JSON) nodes - JavaScript
up vote
0
down vote
favorite
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?
javascript json object
add a comment |
up vote
0
down vote
favorite
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?
javascript json object
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
javascript json object
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?
javascript json object
javascript json object
asked Nov 19 at 6:22
Duke
247
247
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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/
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
add a comment |
up vote
0
down vote
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];
});
Thanks, but if initial objectjsonObject
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
– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?
– Duke
Nov 19 at 18:45
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
– Manish
Nov 20 at 9:09
No, it's not a duplicate."is_search"
and"is_author"
is not the same.
– Duke
Nov 20 at 10:23
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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/
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
add a comment |
up vote
0
down vote
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/
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
add a comment |
up vote
0
down vote
up vote
0
down vote
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/
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/
answered Nov 19 at 6:25
Aasif khan
25
25
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
add a comment |
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
Nope, even this does not work. Please see my comment on @Manish post.
– Duke
Nov 19 at 10:40
add a comment |
up vote
0
down vote
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];
});
Thanks, but if initial objectjsonObject
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
– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?
– Duke
Nov 19 at 18:45
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
– Manish
Nov 20 at 9:09
No, it's not a duplicate."is_search"
and"is_author"
is not the same.
– Duke
Nov 20 at 10:23
|
show 1 more comment
up vote
0
down vote
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];
});
Thanks, but if initial objectjsonObject
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
– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?
– Duke
Nov 19 at 18:45
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
– Manish
Nov 20 at 9:09
No, it's not a duplicate."is_search"
and"is_author"
is not the same.
– Duke
Nov 20 at 10:23
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
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];
});
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];
});
answered Nov 19 at 7:21
Manish
55
55
Thanks, but if initial objectjsonObject
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
– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?
– Duke
Nov 19 at 18:45
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
– Manish
Nov 20 at 9:09
No, it's not a duplicate."is_search"
and"is_author"
is not the same.
– Duke
Nov 20 at 10:23
|
show 1 more comment
Thanks, but if initial objectjsonObject
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
– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?
– Duke
Nov 19 at 18:45
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
– Manish
Nov 20 at 9:09
No, it's not a duplicate."is_search"
and"is_author"
is not the same.
– Duke
Nov 20 at 10:23
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– Duke
Nov 19 at 10:29
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– Duke
Nov 19 at 10:29
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
Can you check if you need this jsbin.com/hopefenapu/edit?js,console
– Manish
Nov 19 at 11:59
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?– Duke
Nov 19 at 18:45
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?– Duke
Nov 19 at 18:45
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– Manish
Nov 20 at 9:09
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– Manish
Nov 20 at 9:09
No, it's not a duplicate.
"is_search"
and "is_author"
is not the same.– Duke
Nov 20 at 10:23
No, it's not a duplicate.
"is_search"
and "is_author"
is not the same.– Duke
Nov 20 at 10:23
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369305%2fdelete-duplicate-objectjson-nodes-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown