How to get into an object that is in an array

How to get into an object that is in an array



Let's say I have an array like this :


arrayOfObject = [item: this: is, that: is, item2: this: is, that: is]



I'm trying to access item and item2 without having to use a 0/1 index. I'd like to be able to say arrayOfObjects[item] to get into the object. Is this possible?





Your syntax is wrong here, so it's hard to understand what exactly the structure is. But the bottom line is simple: when you need to address item by some arbitrary identifier, use Object (or Map). Otherwise you'll have to scan the whole array (with .find() or similar op) each time when trying to access specific item.
– raina77ow
Aug 30 at 17:06



.find()




4 Answers
4



You can not do exactly this but something similar where you would "convert" your array to an object and then use the keys to access the values:




arrayOfObject = [ item: this: "a", that: "b" , item2: this: "c", that: "d" ]

const arrayToObject = arrayOfObject.reduce((r,c) => Object.assign(r,c), )

console.log(arrayToObject['item'])
console.log(arrayToObject['item2'])



In the snippet above we convert the arrayOfObject to arrayToObject and then simply access the values via the keys.


arrayOfObject



Otherwise what you are trying to do is not possible since you can only access values from an array by index or via some kind of a function which would traverse it and get you the entry, like find etc.


find





Found this very helpful and exactly what I was looking for. Thank you!
– fsp712
Aug 30 at 19:02





Great. Happy to help. If this was helpful please consider up-voting or simply accepting this as the answer. This would help other people having similar question finding the answer quicker.
– Akrion
Aug 30 at 19:04





Would love to but I am just shy of the reputation needed (I have 14!). Definitely will once my reputation is high enough. Thanks!
– fsp712
Aug 30 at 20:26





@fsp712 but you can accept this answer by clicking on right tick.
– Nishant Dixit
Aug 31 at 4:56





Done. Thanks for your help!
– fsp712
Sep 3 at 17:45




You can use Array.find.




arrayOfObject = [
item:
this: 'is',
that: 'is'

,
item2:
this: 'is',
that: 'is'

]

console.log(arrayOfObject.find(ob => ob['item']));
console.log(arrayOfObject.find(ob => ob['item2']));





@DanielHilgarth Updated the code.. I think the OP is trying to tell us what the logic is without much caring about the syntaxes. But he should also care the syntax too IMO.
– Arup Rakshit
Aug 30 at 17:09




var arrayOfObject = [
"item":
"this": "is",
"that": "is"

,
"item2":
"this": "is",
"that": "is"

];

var itemObject = ;
arrayOfObject.forEach(function(value)
var filterObject = Object.keys(value).filter(val => val.indexOf("item") != -1);
if (filterObject.length > 0)
filterObject.forEach(key => );

);

console.log(itemObject.item); //item
console.log(itemObject.item2); //item



Yeah sure it is possible:


var result = arrayOfObject.map(a => a.item);



or


var result = arrayOfObject.map(a => a.item2);



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ