More direct way to return object's specific value from an array in javascript?Best way to find if an item is in a JavaScript array?Remove empty elements from an array in JavascriptHow to insert an item into an array at a specific index (JavaScript)?Get all unique values in a JavaScript array (remove duplicates)Return multiple values in JavaScript?How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Get random item from JavaScript arrayRemove duplicate values from JS arrayGet JavaScript object from array of objects by value of property
Is the destination of a commercial flight important for the pilot?
What to do with wrong results in talks?
What is the difference between "behavior" and "behaviour"?
How do I find the solutions of the following equation?
Customer Requests (Sometimes) Drive Me Bonkers!
How do I go from 300 unfinished/half written blog posts, to published posts?
Go Pregnant or Go Home
Why Were Madagascar and New Zealand Discovered So Late?
Unexpected indention in bibliography items (beamer)
How did Arya survive the stabbing?
Is a stroke of luck acceptable after a series of unfavorable events?
Closest Prime Number
Term for the "extreme-extension" version of a straw man fallacy?
How does buying out courses with grant money work?
Do sorcerers' subtle spells require a skill check to be unseen?
System.debug(JSON.Serialize(o)) Not longer shows full string
Is `x >> pure y` equivalent to `liftM (const y) x`
How to write papers efficiently when English isn't my first language?
Sequence of Tenses: Translating the subjunctive
How did Doctor Strange see the winning outcome in Avengers: Infinity War?
Was Spock the First Vulcan in Starfleet?
Proof of work - lottery approach
How to pronounce the slash sign
Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?
More direct way to return object's specific value from an array in javascript?
Best way to find if an item is in a JavaScript array?Remove empty elements from an array in JavascriptHow to insert an item into an array at a specific index (JavaScript)?Get all unique values in a JavaScript array (remove duplicates)Return multiple values in JavaScript?How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Get random item from JavaScript arrayRemove duplicate values from JS arrayGet JavaScript object from array of objects by value of property
So I encountered a situation which left me wanting a different solution.
I have an immutable array of objects.
[
id: 0,
value:10
,
id: 1,
value:20
,
id: 2,
value:20
,
]
and I needed to search through the array, find the object with my id, and then return a single value from within that object.
What I ended up doing:
// pull out the entire object from the array
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
// set up a temp var to store the desired value
let tempValue = 0;
// make sure the object is valid
if(tempObject !-- undefined)
// finally store my value
tempValue = tempObject.value;
This just seems like a waste. Storing an entire object just to get a single value?
I feel like it should be something like
const myValue = immutableArray.toJS().find(elem => (elem.id === myId).value);
or
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
But obviously that doesn't work.
Is there a more direct way like this to access this value without storing the whole object?
javascript arrays immutable.js
add a comment |
So I encountered a situation which left me wanting a different solution.
I have an immutable array of objects.
[
id: 0,
value:10
,
id: 1,
value:20
,
id: 2,
value:20
,
]
and I needed to search through the array, find the object with my id, and then return a single value from within that object.
What I ended up doing:
// pull out the entire object from the array
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
// set up a temp var to store the desired value
let tempValue = 0;
// make sure the object is valid
if(tempObject !-- undefined)
// finally store my value
tempValue = tempObject.value;
This just seems like a waste. Storing an entire object just to get a single value?
I feel like it should be something like
const myValue = immutableArray.toJS().find(elem => (elem.id === myId).value);
or
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
But obviously that doesn't work.
Is there a more direct way like this to access this value without storing the whole object?
javascript arrays immutable.js
2
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
– Pranav C Balan
Mar 21 at 15:50
2
I would add a fallback to that:const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0
– Get Off My Lawn
Mar 21 at 15:51
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0
– Pranav C Balan
Mar 21 at 15:54
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57
add a comment |
So I encountered a situation which left me wanting a different solution.
I have an immutable array of objects.
[
id: 0,
value:10
,
id: 1,
value:20
,
id: 2,
value:20
,
]
and I needed to search through the array, find the object with my id, and then return a single value from within that object.
What I ended up doing:
// pull out the entire object from the array
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
// set up a temp var to store the desired value
let tempValue = 0;
// make sure the object is valid
if(tempObject !-- undefined)
// finally store my value
tempValue = tempObject.value;
This just seems like a waste. Storing an entire object just to get a single value?
I feel like it should be something like
const myValue = immutableArray.toJS().find(elem => (elem.id === myId).value);
or
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
But obviously that doesn't work.
Is there a more direct way like this to access this value without storing the whole object?
javascript arrays immutable.js
So I encountered a situation which left me wanting a different solution.
I have an immutable array of objects.
[
id: 0,
value:10
,
id: 1,
value:20
,
id: 2,
value:20
,
]
and I needed to search through the array, find the object with my id, and then return a single value from within that object.
What I ended up doing:
// pull out the entire object from the array
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
// set up a temp var to store the desired value
let tempValue = 0;
// make sure the object is valid
if(tempObject !-- undefined)
// finally store my value
tempValue = tempObject.value;
This just seems like a waste. Storing an entire object just to get a single value?
I feel like it should be something like
const myValue = immutableArray.toJS().find(elem => (elem.id === myId).value);
or
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
But obviously that doesn't work.
Is there a more direct way like this to access this value without storing the whole object?
javascript arrays immutable.js
javascript arrays immutable.js
edited Mar 21 at 15:52
bauervision
asked Mar 21 at 15:49
bauervisionbauervision
7713
7713
2
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
– Pranav C Balan
Mar 21 at 15:50
2
I would add a fallback to that:const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0
– Get Off My Lawn
Mar 21 at 15:51
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0
– Pranav C Balan
Mar 21 at 15:54
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57
add a comment |
2
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;
– Pranav C Balan
Mar 21 at 15:50
2
I would add a fallback to that:const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0
– Get Off My Lawn
Mar 21 at 15:51
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0
– Pranav C Balan
Mar 21 at 15:54
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57
2
2
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;– Pranav C Balan
Mar 21 at 15:50
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;– Pranav C Balan
Mar 21 at 15:50
2
2
I would add a fallback to that:
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0– Get Off My Lawn
Mar 21 at 15:51
I would add a fallback to that:
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0– Get Off My Lawn
Mar 21 at 15:51
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0– Pranav C Balan
Mar 21 at 15:54
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0– Pranav C Balan
Mar 21 at 15:54
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57
add a comment |
2 Answers
2
active
oldest
votes
a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0
perfect thank you
– bauervision
Mar 21 at 16:02
add a comment |
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
const getValue = tempObject ? tempObject.value : 0
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%2f55284331%2fmore-direct-way-to-return-objects-specific-value-from-an-array-in-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
a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0
perfect thank you
– bauervision
Mar 21 at 16:02
add a comment |
a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0
perfect thank you
– bauervision
Mar 21 at 16:02
add a comment |
a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0a = [id: 0, value:10, id: 1, value:20, id: 2, value:20];
console.log((a.find(e => e.id == 1) || value: 0).value); // 20
console.log((a.find(e => e.id == 3) || value: 0).value); // 0edited Mar 21 at 15:58
answered Mar 21 at 15:56
Mikhail VladimirovMikhail Vladimirov
11.2k12631
11.2k12631
perfect thank you
– bauervision
Mar 21 at 16:02
add a comment |
perfect thank you
– bauervision
Mar 21 at 16:02
perfect thank you
– bauervision
Mar 21 at 16:02
perfect thank you
– bauervision
Mar 21 at 16:02
add a comment |
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
const getValue = tempObject ? tempObject.value : 0
add a comment |
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
const getValue = tempObject ? tempObject.value : 0
add a comment |
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
const getValue = tempObject ? tempObject.value : 0
const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));
const getValue = tempObject ? tempObject.value : 0
answered Mar 21 at 15:54
richard nelsonrichard nelson
936
936
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.
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%2f55284331%2fmore-direct-way-to-return-objects-specific-value-from-an-array-in-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
2
there is a typo in your code, you are trying to get value property from boolean value and returning it(which would be always falsy value(undefined))...
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value;– Pranav C Balan
Mar 21 at 15:50
2
I would add a fallback to that:
const myValue = immutableArray.toJS().find(elem => (elem.id === myId)).value || 0– Get Off My Lawn
Mar 21 at 15:51
Fallback value should probably be zero instead of empty string.
– Mikhail Vladimirov
Mar 21 at 15:52
const myValue = (immutableArray.toJS().find(elem => (elem.id === myId)) || value : 0).value || 0– Pranav C Balan
Mar 21 at 15:54
@GetOffMyLawn if the find doesn’t find anything it will return an error when trying to access .value
– richard nelson
Mar 21 at 15:57