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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 6:22









      Duke

      247




      247
























          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/






          share|improve this answer





















          • Nope, even this does not work. Please see my comment on @Manish post.
            – Duke
            Nov 19 at 10:40


















          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];
          });





          share|improve this answer





















          • 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












          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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/






          share|improve this answer





















          • Nope, even this does not work. Please see my comment on @Manish post.
            – Duke
            Nov 19 at 10:40















          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/






          share|improve this answer





















          • Nope, even this does not work. Please see my comment on @Manish post.
            – Duke
            Nov 19 at 10:40













          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/






          share|improve this answer












          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/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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












          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];
          });





          share|improve this answer





















          • 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












          • 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















          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];
          });





          share|improve this answer





















          • 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












          • 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













          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];
          });





          share|improve this answer












          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];
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 7:21









          Manish

          55




          55












          • 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












          • 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










          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga