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













-1















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?










share|improve this question



















  • 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















-1















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?










share|improve this question



















  • 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













-1












-1








-1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












2 Answers
2






active

oldest

votes


















1

















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








share|improve this answer

























  • perfect thank you

    – bauervision
    Mar 21 at 16:02


















1














const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));

const getValue = tempObject ? tempObject.value : 0





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%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









    1

















    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








    share|improve this answer

























    • perfect thank you

      – bauervision
      Mar 21 at 16:02















    1

















    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








    share|improve this answer

























    • perfect thank you

      – bauervision
      Mar 21 at 16:02













    1












    1








    1










    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








    share|improve this answer


















    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








    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





    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 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

















    • 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













    1














    const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));

    const getValue = tempObject ? tempObject.value : 0





    share|improve this answer



























      1














      const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));

      const getValue = tempObject ? tempObject.value : 0





      share|improve this answer

























        1












        1








        1







        const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));

        const getValue = tempObject ? tempObject.value : 0





        share|improve this answer













        const tempObject = immutableArray.toJS().find(elem => (elem.id === myId));

        const getValue = tempObject ? tempObject.value : 0






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 15:54









        richard nelsonrichard nelson

        936




        936



























            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.




            draft saved


            draft discarded














            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





















































            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

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?