How to search item using key in object and update that value into the array?
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..
javascript arrays
add a comment |
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..
javascript arrays
add a comment |
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..
javascript arrays
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
javascript arrays
edited Nov 12 '18 at 14:45
Gordon A.
627
627
asked Nov 12 '18 at 13:13
Sachin
102
102
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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);
add a comment |
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)
add a comment |
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]];
}
});
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
add a comment |
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)
add a comment |
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
});
}
});
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%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
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 12 '18 at 13:20
Artyom Amiryan
1,825113
1,825113
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 '18 at 13:21
Nitish Narang
2,948815
2,948815
add a comment |
add a comment |
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]];
}
});
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
add a comment |
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]];
}
});
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
add a comment |
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]];
}
});
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]];
}
});
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 '18 at 13:31
Mohammed Ashfaq
1,6672512
1,6672512
add a comment |
add a comment |
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.
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%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
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