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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

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