How to search item using key in object and update that value into the array?












1

















var replaceArr = ['A','J','Q',10,2];

var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];





As per the snippet I have two array:
replaceArr and originalArr



I want to compare the replaceArr with the originalArr and get the key value from there and replace into the replaceArr.
Means after replacing the replaceArr would be == [0,10,11,9,1]



In Advanced Thanks..










share|improve this question





























    1

















    var replaceArr = ['A','J','Q',10,2];

    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];





    As per the snippet I have two array:
    replaceArr and originalArr



    I want to compare the replaceArr with the originalArr and get the key value from there and replace into the replaceArr.
    Means after replacing the replaceArr would be == [0,10,11,9,1]



    In Advanced Thanks..










    share|improve this question



























      1












      1








      1










      var replaceArr = ['A','J','Q',10,2];

      var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];





      As per the snippet I have two array:
      replaceArr and originalArr



      I want to compare the replaceArr with the originalArr and get the key value from there and replace into the replaceArr.
      Means after replacing the replaceArr would be == [0,10,11,9,1]



      In Advanced Thanks..










      share|improve this question


















      var replaceArr = ['A','J','Q',10,2];

      var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];





      As per the snippet I have two array:
      replaceArr and originalArr



      I want to compare the replaceArr with the originalArr and get the key value from there and replace into the replaceArr.
      Means after replacing the replaceArr would be == [0,10,11,9,1]



      In Advanced Thanks..






      var replaceArr = ['A','J','Q',10,2];

      var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];





      var replaceArr = ['A','J','Q',10,2];

      var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];






      javascript arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:45









      Gordon A.

      627




      627










      asked Nov 12 '18 at 13:13









      Sachin

      102




      102
























          4 Answers
          4






          active

          oldest

          votes


















          1














          you can simple use map and find from array prototype




          var replaceArr = ["A", "J","Q",10,2]

          var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

          replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
          console.log(replaceArr);








          share|improve this answer





























            0














            You can club all the different objects in originalArr and then search for replaceArr values in it like below






            var replaceArr = ['A','J','Q',10,2]

            var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

            let arrWithOneObject = Object.assign(...originalArr)

            let result = replaceArr.map(d => arrWithOneObject[d])

            console.log(result)








            share|improve this answer





























              0














              You can use javascript array foreach function



              originalArr.forEach((val) => {
              var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]);
              if (replaceIndex >= 0) {
              replaceArr[replaceIndex] = val[Object.keys(val)[0]];
              }
              });





              share|improve this answer





















              • it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                – Artyom Amiryan
                Nov 12 '18 at 13:30










              • seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                – Ali Khan
                Nov 12 '18 at 14:14





















              0

















              var replaceArr = ['A','J','Q','10','2']			
              var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

              originalArr.forEach((item)=>{
              let property = Object.keys(item)[0];
              let indexOfproperty = replaceArr.indexOf(property);
              if(indexOfproperty !== -1){
              replaceArr[indexOfproperty] = item[property]
              }
              })

              console.log(replaceArr)








              share|improve this answer





















                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',
                autoActivateHeartbeat: false,
                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%2f53262945%2fhow-to-search-item-using-key-in-object-and-update-that-value-into-the-array%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                you can simple use map and find from array prototype




                var replaceArr = ["A", "J","Q",10,2]

                var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                console.log(replaceArr);








                share|improve this answer


























                  1














                  you can simple use map and find from array prototype




                  var replaceArr = ["A", "J","Q",10,2]

                  var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                  replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                  console.log(replaceArr);








                  share|improve this answer
























                    1












                    1








                    1






                    you can simple use map and find from array prototype




                    var replaceArr = ["A", "J","Q",10,2]

                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                    replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                    console.log(replaceArr);








                    share|improve this answer












                    you can simple use map and find from array prototype




                    var replaceArr = ["A", "J","Q",10,2]

                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                    replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                    console.log(replaceArr);








                    var replaceArr = ["A", "J","Q",10,2]

                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                    replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                    console.log(replaceArr);





                    var replaceArr = ["A", "J","Q",10,2]

                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];

                    replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
                    console.log(replaceArr);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 13:20









                    Artyom Amiryan

                    1,825113




                    1,825113

























                        0














                        You can club all the different objects in originalArr and then search for replaceArr values in it like below






                        var replaceArr = ['A','J','Q',10,2]

                        var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                        let arrWithOneObject = Object.assign(...originalArr)

                        let result = replaceArr.map(d => arrWithOneObject[d])

                        console.log(result)








                        share|improve this answer


























                          0














                          You can club all the different objects in originalArr and then search for replaceArr values in it like below






                          var replaceArr = ['A','J','Q',10,2]

                          var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                          let arrWithOneObject = Object.assign(...originalArr)

                          let result = replaceArr.map(d => arrWithOneObject[d])

                          console.log(result)








                          share|improve this answer
























                            0












                            0








                            0






                            You can club all the different objects in originalArr and then search for replaceArr values in it like below






                            var replaceArr = ['A','J','Q',10,2]

                            var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                            let arrWithOneObject = Object.assign(...originalArr)

                            let result = replaceArr.map(d => arrWithOneObject[d])

                            console.log(result)








                            share|improve this answer












                            You can club all the different objects in originalArr and then search for replaceArr values in it like below






                            var replaceArr = ['A','J','Q',10,2]

                            var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                            let arrWithOneObject = Object.assign(...originalArr)

                            let result = replaceArr.map(d => arrWithOneObject[d])

                            console.log(result)








                            var replaceArr = ['A','J','Q',10,2]

                            var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                            let arrWithOneObject = Object.assign(...originalArr)

                            let result = replaceArr.map(d => arrWithOneObject[d])

                            console.log(result)





                            var replaceArr = ['A','J','Q',10,2]

                            var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                            let arrWithOneObject = Object.assign(...originalArr)

                            let result = replaceArr.map(d => arrWithOneObject[d])

                            console.log(result)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 13:21









                            Nitish Narang

                            2,948815




                            2,948815























                                0














                                You can use javascript array foreach function



                                originalArr.forEach((val) => {
                                var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]);
                                if (replaceIndex >= 0) {
                                replaceArr[replaceIndex] = val[Object.keys(val)[0]];
                                }
                                });





                                share|improve this answer





















                                • it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                  – Artyom Amiryan
                                  Nov 12 '18 at 13:30










                                • seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                  – Ali Khan
                                  Nov 12 '18 at 14:14


















                                0














                                You can use javascript array foreach function



                                originalArr.forEach((val) => {
                                var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]);
                                if (replaceIndex >= 0) {
                                replaceArr[replaceIndex] = val[Object.keys(val)[0]];
                                }
                                });





                                share|improve this answer





















                                • it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                  – Artyom Amiryan
                                  Nov 12 '18 at 13:30










                                • seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                  – Ali Khan
                                  Nov 12 '18 at 14:14
















                                0












                                0








                                0






                                You can use javascript array foreach function



                                originalArr.forEach((val) => {
                                var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]);
                                if (replaceIndex >= 0) {
                                replaceArr[replaceIndex] = val[Object.keys(val)[0]];
                                }
                                });





                                share|improve this answer












                                You can use javascript array foreach function



                                originalArr.forEach((val) => {
                                var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]);
                                if (replaceIndex >= 0) {
                                replaceArr[replaceIndex] = val[Object.keys(val)[0]];
                                }
                                });






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 12 '18 at 13:28









                                Ali Khan

                                607




                                607












                                • it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                  – Artyom Amiryan
                                  Nov 12 '18 at 13:30










                                • seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                  – Ali Khan
                                  Nov 12 '18 at 14:14




















                                • it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                  – Artyom Amiryan
                                  Nov 12 '18 at 13:30










                                • seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                  – Ali Khan
                                  Nov 12 '18 at 14:14


















                                it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                – Artyom Amiryan
                                Nov 12 '18 at 13:30




                                it displays wrong result [0, 10, 11, 10, 2], the correct result should be [0,10,11,9,1]
                                – Artyom Amiryan
                                Nov 12 '18 at 13:30












                                seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                – Ali Khan
                                Nov 12 '18 at 14:14






                                seemed to be working on my side var replaceArr = ['A','J','Q','10','2'] var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}] originalArr.forEach((val) => { var replaceIndex = replaceArr.indexOf(Object.keys(val)[0]); if (replaceIndex >= 0) { replaceArr[replaceIndex] = val[Object.keys(val)[0]]; } });console.log(replaceArr);
                                – Ali Khan
                                Nov 12 '18 at 14:14













                                0

















                                var replaceArr = ['A','J','Q','10','2']			
                                var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                originalArr.forEach((item)=>{
                                let property = Object.keys(item)[0];
                                let indexOfproperty = replaceArr.indexOf(property);
                                if(indexOfproperty !== -1){
                                replaceArr[indexOfproperty] = item[property]
                                }
                                })

                                console.log(replaceArr)








                                share|improve this answer


























                                  0

















                                  var replaceArr = ['A','J','Q','10','2']			
                                  var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                  originalArr.forEach((item)=>{
                                  let property = Object.keys(item)[0];
                                  let indexOfproperty = replaceArr.indexOf(property);
                                  if(indexOfproperty !== -1){
                                  replaceArr[indexOfproperty] = item[property]
                                  }
                                  })

                                  console.log(replaceArr)








                                  share|improve this answer
























                                    0












                                    0








                                    0









                                    var replaceArr = ['A','J','Q','10','2']			
                                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                    originalArr.forEach((item)=>{
                                    let property = Object.keys(item)[0];
                                    let indexOfproperty = replaceArr.indexOf(property);
                                    if(indexOfproperty !== -1){
                                    replaceArr[indexOfproperty] = item[property]
                                    }
                                    })

                                    console.log(replaceArr)








                                    share|improve this answer















                                    var replaceArr = ['A','J','Q','10','2']			
                                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                    originalArr.forEach((item)=>{
                                    let property = Object.keys(item)[0];
                                    let indexOfproperty = replaceArr.indexOf(property);
                                    if(indexOfproperty !== -1){
                                    replaceArr[indexOfproperty] = item[property]
                                    }
                                    })

                                    console.log(replaceArr)








                                    var replaceArr = ['A','J','Q','10','2']			
                                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                    originalArr.forEach((item)=>{
                                    let property = Object.keys(item)[0];
                                    let indexOfproperty = replaceArr.indexOf(property);
                                    if(indexOfproperty !== -1){
                                    replaceArr[indexOfproperty] = item[property]
                                    }
                                    })

                                    console.log(replaceArr)





                                    var replaceArr = ['A','J','Q','10','2']			
                                    var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}]

                                    originalArr.forEach((item)=>{
                                    let property = Object.keys(item)[0];
                                    let indexOfproperty = replaceArr.indexOf(property);
                                    if(indexOfproperty !== -1){
                                    replaceArr[indexOfproperty] = item[property]
                                    }
                                    })

                                    console.log(replaceArr)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 '18 at 13:31









                                    Mohammed Ashfaq

                                    1,6672512




                                    1,6672512






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262945%2fhow-to-search-item-using-key-in-object-and-update-that-value-into-the-array%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

                                        Florida Star v. B. J. F.

                                        Danny Elfman

                                        Lugert, Oklahoma