This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 7 months ago.
I have array (API response):
let arr = [
{ '#type': 'Something', data: 1234 },
{ '#type': 'Something', data: 3214 },
]
Is it possible to destructure elements with those '#' prefixed fields?
for (const { data, ??? #type } of arr) {}
You could take a computed property and a new variable name.
let arr = [{ '#type': 'Something', data: 1234 }, { '#type': 'Something', data: 3214 }];
for (const { data, ['#type']: renamed } of arr) {
console.log(renamed);
}
Related
This question already has answers here:
How to combine two arrays into an array of objects in javascript?
(2 answers)
Combine the values of two arrays into object
(5 answers)
How to create a JS object from arrays [duplicate]
(4 answers)
Closed 6 months ago.
Is there a nice way of transforming a flat javascript array into an array of objects?
An example would be to transform this array.
I'm stuck in trying to merge the two arrays and create an object with the values.
const dataSet = [
{
last_updated: 1662040601,
x: [
1660953600, 1661040000, 1661126400, 1661212800, 1661299200, 1661385600,
1661472000, 1661558400, 1661644800, 1661731200, 1661817600, 1661904000,
],
y: [
0.07, 0.062, 0.06, 0.0725, 0.075, 0.089, 0.0799, 0.1167, 0.089, 0.08,
0.077, 0.0639,
],
},
];
Into this:
const array = [
{ data: 1660953600, value: 0.07 },
{ data: 1661040000, value: 0.062 },
{ data: 1661126400, value: 0.06 },
{ data: 1661212800, value: 0.0725 },
];
My attempt:
const arri = dataSet.map((data) => ({
data: data.x.map((data) => data),
value: data.y,
}));
I hope it will help you..
const array = dataSet[0].x.map((x, i) => ({ data: x, value: dataSet[0].y[i] }));
What do you think about it ?
This question already has answers here:
How to traverse through array within an object and return each array element as the second key value pair, along with its' corresponding ID
(2 answers)
Closed 1 year ago.
i have an array
let data = [
{
A_ID: 'ABC',
C_Array: ["123","456","789"]
},
{
A_ID: 'DEF',
C_Array: ["444","555","666"]
}
]
and i want to create a new array like so that for each C_Array i get its coresponding A_ID,
like
let newData = [
{
A_ID: 'ABC',
C_ArrayValue: "123"
},
{
A_ID: 'ABC',
C_ArrayValue: "456"
},
]
How can i do that in JS
let data = [
{
A_ID: 'ABC',
C_Array: ["123", "456", "789"]
},
{
A_ID: 'DEF',
C_Array: ["444", "555", "666"]
}
]
const newArray = data.map((outerNode) => {
return outerNode.C_Array.map((innerNode) => {
return {
A_ID: outerNode.A_ID,
C_ArrayValue: innerNode
}
})
});
console.log(newArray);
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
How can i create another array by filtering one json array and include just the values pairs of a specific key?
Example (Filtering Value Pairs of Number Keys):
Array:
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]````
Result Array:
````var filteredarray = ['1234','5678'];````
Thanks!
const a = [
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]
function getNumbers(){
return a.map(item => item.Number);
}
getNumbers();
This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 4 years ago.
I have an array of objects which I'm using the .includes() function. I'm searching this array with an object that is in the array (Objects are identical). However there doesn't appear to be a match. I have replicated the problem in this fiddle. The code is also below. So what is the correct way to check if an array contains am object?
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
if (list1.includes({
name: "object1"
})) {
document.write('contains')
} else {
document.write('doesnt')
}
You can't compare objects directly, but using this method , you can compare them with JSON.stringify.
let list1 = [{
name: "object1"
},
{
name: "object2"
},
{
name: "object3"
},
{
name: "object4"
}
]
var contains = list1.some(elem =>{
return JSON.stringify({name: "object1"}) === JSON.stringify(elem);
});
if (contains) {
document.write('contains')
} else {
document.write('doesnt')
}
You can try following
let list1 = [{name:"object1"},{name:"object2"},{name:"object3"},{name:"object4"}]
if (list1.some(({name}) => name === "object1")) {
document.write('contains')
} else {
document.write('doesnt')
}
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
NOT duplicate of : Dynamically access object property using variable
How to read the JavaScript Object Array property dynamically.
var person = {
name: "Ravi",
age: 25
friends: [{
name: "Suresh"
},
{
name: "Nitin"
},
{
name: "Argha"
}
]
}
So, if I want to read any property dynamically, I can use
var dynamicProperty = 'age';
person[dynamicProperty] // Output : 25
But it fails for array.
var dynamicProperty = 'friends[1]';
person[dynamicProperty].name // Output : undefined
What is the best way to pass the name of the array dynamically ?
You can't access more than a single property at a time using dynamic property access notation. You will need to use an array of keys (often called a "path") in conjunction with Array#reduce:
var person = {
name: "Ravi",
age: 25,
friends: [{
name: "Suresh"
},
{
name: "Nitin"
},
{
name: "Argha"
}
]
}
function access (o, k) { return o[k] }
var result = ['friends', 1, 'name'].reduce(access, person)
console.log(result)