This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Want to know how we can access the items in an object when passed to a function :
const sortArrayObj = (arrayObj, field) => {
//How to access "arrayObj[0].field" ???
// Tried to do console.log(`${arrayObj[0]}`) and it gave output as [Object object]
//Now how can i access "name" and "date" from it using the "field" variable ?
};
const arr = [
{ name: "A", date: "13-12-1996" },
{ name: "G", date: "03-02-2020" },
{ name: "C", date: "09-05-2021" },
];
sortArrayObj(arr,"date")
Try in the following way:
const sortArrayObj = (arrayObj, field) => {
// How to access "arrayObj[0].field" ???
arrayObj[0][field];
};
Related
This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
How will you access symbol property here in JS
console.log(hi[0]) outputs { symbol: "asdf" , name:"adad"}
but
console.log(hi[0].symbol) throws the error that symbol is undefined
hi is an array. First you need to access the object so hi[0] will provide the first object. Then access the object using the key which is 0 like below
const hi = [{
0: {
symbol: "asdf",
name: "adad"
}
}];
console.log(hi[0][0].symbol)
hi variable is an Array so you have to access the first element of the array first, then the Object property:
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
console.log(hi[0][0].symbol)
Like this?
console.log(hi[0][0].symbol)
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
hi[0][0].symbol is right.
console.log(hi[0]) => 0: {symbol: "asdf", name: "adad"}
console.log(hi[0][0]) => {symbol: "asdf", name: "adad"}
console.log(hi[0][0].symbol) => "asdf"
This question already has answers here:
Check if a value exists in an Array object in JavaScript or Angular
(4 answers)
Closed 3 years ago.
I want to return true if a nested array contains a particular value
In this example I'm trying to see if the users array has the current users id but I get the object instead of true
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.find(user=>user.id === currentUserId))
The problem is you are using .find() instead of .some(). Try the following:
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.some(user=>user.id === currentUserId))
The difference is in the output. .find() will return the value, .some() will return a boolean.
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I'm trying to access the properties of easeSegmentNameMap using:
easeSegmentNameMap["EMAIL_ENGAGED"].text
I'm getting undefined, it works if I use:
easeSegmentNameMap["EMAIL_ENGAGED"].name
var easeSegmentNameMap = {
"EMAIL_ENGAGED": {
name: "emailEngaged"
},
"EMAIL_INACTIVE": {
name: "emailInactive"
},
"LIKELY_TO_THRIVE": {
name: "likelyToThrive"
},
"MOST_VALUABLE_SUBSCRIBERS": {
name: "mostValuableSubscribers"
},
"NEARLY_INACTIVE": {
name: "nearlyInActive"
},
"NEVER_ACTIVATED": {
name: "neverActivated"
},
"QUESTION_MARKS": {
name: "questionMarks"
}
};
var text = "name";
alert(easeSegmentNameMap["EMAIL_ENGAGED"].text);
Output:
undefined
What am I doing wrong?
Try this:
alert(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
text is a variable and has to be interpolated.
You can't access
var text="name";
alert(easeSegmentNameMap["EMAIL_ENGAGED"].text);
the text is still the property, in this case, it's not the variable.
If you wanted to access the variable you should access like this:
alert(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
Just need to use [] bracket notation. whenever you need to access any object property using variable you need to use [] notation.
var easeSegmentNameMap = {
"EMAIL_ENGAGED": {
name: "emailEngaged"
},
"EMAIL_INACTIVE": {
name: "emailInactive"
},
"LIKELY_TO_THRIVE": {
name: "likelyToThrive"
},
"MOST_VALUABLE_SUBSCRIBERS": {
name: "mostValuableSubscribers"
},
"NEARLY_INACTIVE": {
name: "nearlyInActive"
},
"NEVER_ACTIVATED": {
name: "neverActivated"
},
"QUESTION_MARKS": {
name: "questionMarks"
}
};
var text = "name";
console.log(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
This question already has answers here:
Convert Array to Object
(46 answers)
Closed 6 years ago.
I have an array, say [{ id: 'first' }, { id: 'second' }]
Are there any native methods I'm missing that will convert the array into an array-like object? I've seen plenty of answers asking about the other direction, and recommending Array.prototype.slice.call (or the newer Array.from), but I can't find anyone asking about this. For the above example, I'd want to get the following output:
{
0: { id: 'first' }
1: { id: 'second' }
}
Reducing it
var array = [{ id: 'first' }, { id: 'second' }];
var obj = array.reduce( (a,b,i) => {return a[i]=b,a},{})
console.log(obj)
Check out the documentation on Array.reduce on MDN (Mozilla Developer Network)
You could just iterate through the array and add the values to the respective index
var arr = [{ id: 'first' }, { id: 'second' }];
var set = {};
arr.forEach(function(value, index){
set[index] = value;
})
console.log(set)
Not a native method, but the following helper function should do the trick:
var arr = [{ id: 'first' }, { id: 'second' }];
function toArrayLike(arrayToConvert) {
var retVal = {};
Object.keys(arrayToConvert).forEach(function(key) {
retVal[key] = arrayToConvert[key];
});
return retVal
}
var arrayLike = toArrayLike(arr);
console.log(arrayLike);