This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 2 years ago.
This is what my array looks like:
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
And I have the uuid key in the variable:
const uuid = '111-111-111';
Now based on this uuid, I would like to extract the value from the amountMoney: 44.04.
How do you write this in a nice way in js?
You can use Array.prototype.find:
items.find(item => item.uuid === uuid) // -> found object
Use Array.prototype.find to find the object if the property uuid of the object matches the value of the variable uuid. Before extracting the value for amountMoney check if the object was found.
Example,
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
const uuid = '111-111-111';
const foundItem = items.find(item => item.uuid === uuid);
if (foundItem) {
console.log(foundItem.amountMoney)
}
Related
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
How to find object in array by property in javascript?
(3 answers)
Closed 4 months ago.
I have an object called _test shaped in the below form where the first element is id and the second is name:
"_test": [
{id:1, name:andy},{id:2, name:james}, {id:3, name:mike}
]
I then have another field called key. the values that key takes on can equal values of id in the subs
key
I currently use
_test.flatMap( c => c.id).find(elem => elem == key) || null
How can I get this to return the name? I'm at a loss and having a major brain fart.
You can find the name for a given id via:
const
_test = [
{ id: 1, name: 'Andy' },
{ id: 2, name: 'James' },
{ id: 3, name: 'Mike' }
],
key = 2,
{ name } = _test.find(({ id }) => id === key) ?? {};
console.log(name); // James
actually is pretty easy, you just need to use find on your array, it takes a function as an argument.
pd: when doing ([id, name]) I'm destructuring the array, you can change that to:
names.find(entry => key === entry[0])
const names = [
[1, 'andy'],
[2, 'james'],
[3, 'mike']
];
const key = 3;
const solution = names.find(([id, name]) => key === id)
console.log("solution => ", solution)
console.log("just the name => ",
solution[1]);
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
Array looks list :
[
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
]
I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.
You can use find like:
const data = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
];
console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));
Reference:
Array.prototype.find()
result = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
].find(item => item.Uid === '673bjhbjhdcsy')
This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 1 year ago.
I want to convert JSON array to a single object. PFB the details
Array:
[{ "item-A": "value-1" }, { "item-B": "value-2" }]
Expected Result:
{ "item-A": "value-1", "item-B": "value-2" }
I have tried following options but result is not what I was expecting
let json = { ...array };
json = Object.assign({}, array);
json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});
Result:
{"0":{"item-A":"value-1"},"1":{"item-B":"value-2"}}
You can use Object.assign and spread the array
const arr=[{ "item-A": "value-1" }, { "item-B": "value-2" }];
console.log(Object.assign({},...arr));
You can use reduce like how you did it with more attention like this:
let array = [{ "item-A": "value-1" }, { "item-B": "value-2" }];
let object = array.reduce((prev, curr) => ({ ...prev, ...curr }), {});
console.log(object);
This question already has answers here:
Javascript: How to Get Object property using Array of string? [duplicate]
(7 answers)
Closed 3 years ago.
I have a config-file:
{
"permission": {
"users": {
"image": {
"data": "example"
}
}
}
}
And an array with a called path like this:
path = ['users', 'image']
How can I get the data?
First try:
config.permission.path[0].path[1];
Second try:
switch (requestedPath[2]) {
case 'users':
switch (requestedPath[3]) {
case 'image':
mydata = config.permission.users["/image"]
}
}
This will work, but is there a better way?
You need a bracket as property accessor for the object, because you take a variable as key.
config.permission[path[0]][path[1]];
For a more dynamic approach, you could reduce the given data with a default object for not fiund properies.
const getV = (object, path) => path.reduce((result, key) => (result || {})[key], object);
var config = { permission: { users: { image: { data: 'example' } } } },
path = ['users', 'image'];
console.log(getV(config.permission, path));
This question already has answers here:
Converting string array to Name/Value object in javascript
(4 answers)
Closed 4 years ago.
How do I convert a string array:
var names = [
"Bob",
"Michael",
"Lanny"
];
into an object like this?
var names = [
{name:"Bob"},
{name:"Michael"},
{name:"Lanny"}
];
Super simple Array.prototype.map() job
names.map(name => ({ name }))
That is... map each entry (name) to an object with key "name" and value name.
var names = [
"Bob",
"Michael",
"Lanny"
];
console.info(names.map(name => ({ name })))
Silly me, I forgot the most important part
names.map(name => name === 'Bob' ? 'Saab' : name)
.map(name => ({ name }))
Use the Array.map() function to map the array to objects. The map() function will iterate through the array and return a new array holding the result of executing the function on each element in the original array. Eg:
names = names.map(function(ele){return {"name":ele}});
You can do this too:
var names = [
"Bob",
"Michael",
"Lanny"
];
var objNames = []
names.forEach(name => {
objNames.push({
name
})
})
Using ES6 you can set name and it is equal to name: name
you can use the map function.
In general, list.map(f) will produce a new list where each element at position i is the result of applying f to the element at the same position in the original list.
For example:
names.map(function(s) {
return {name: s}
});