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 ?
Related
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Convert array of objects into array of properties [duplicate]
(4 answers)
Closed 2 years ago.
Is there a way to transform the following array of objects:
array = [
{ date: "08/18", id: 1 },
{ date: "08/14", id: 2 },
{ date: "08/15", id: 3 }
]
Into this? Only returning the dates:
array2 = ["08/18", "08/14", "08/15"]
I've been trying to push the date elements as it follows, but it doesn's seem to work:
array.map(e => {
array2.push(e.date)
})
Thanks in advance.
You can use the array map function:
array2 = array.map(object => object.date);
This question already has answers here:
best way to convert object with arrays to array with objects and viceversa
(4 answers)
Convert array of objects to object of arrays using lodash
(2 answers)
Closed 2 years ago.
I would like to go from this:
[{
"uuid": 'oaiwngoiasofoi328',
"property1": 3,
"property2": 3,
...many
},
...many ]
to this:
{
"uuid": ['oaiwngoiasofoi328','829rh83hr9h9h29','mejieoainfoi',...],
"property1": [3,3,2,...],
"property2": [3,3,2,...],
...many
}
all the properties will be named the same in each object of the source array
maybe I could use a for loop with map... ??
I've recreated your environment. I think it can help you.
Use Array.reduce() to reduce it to single object.
const data = [
{
uuid: 'adadkljalkd',
'property1': 3,
'property2': 4,
},
{
uuid: 'alkisdfj',
'property1': 4,
'property2': 5,
},
{
uuid: 'klilkkll',
'property1': 7,
'property2': 8,
}
];
const res = data.reduce((acc, obj) => {
Object.keys(obj).forEach(key => {
acc[key] = acc[key] || [];
acc[key].push(obj[key]);
})
return acc;
}, {});
console.log(res);
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:
Javascript string array to object [duplicate]
(4 answers)
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
How do I take a javascript array
var array = [1,2,3,4,5,6]
And convert it to a JSON object that looks like this
[
{
"value": 1
},
{
"value": 2
},
{
"value": 3
},
{
"value": 4
},
{
"value": 5
},
{
"value": 6
}
]
The closest I've come is using this
JSON.stringify(Object.assign({}, array));
But it gives me an output like this
{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6}
You could map the values by taking a short hand property.
var array = [1, 2, 3, 4, 5, 6],
result = array.map(value => ({ value }));
console.log(result);
This question already has answers here:
Changing the key name in an array of objects?
(11 answers)
Closed 5 years ago.
How can I transform values from an array to a new array?
I have an array like:
[
{ "amount" : 10, "date" : "2017-05-30" }...
]
which I want to "transform" into
[
{ x : 10, y: 2017-05-30 }
]
any help is appreciated...
You could map new objects with the new properties.
var array = [{ amount: 10, date: "2017-05-30" }],
newArray = array.map(a => ({ x: a.amount, y: a.date }));
console.log(newArray);
Using Array#map()
The callback function will create a new object with the 2 properties needed. You just have to keep the value of the current array
const array = [{ "amount" : 10, "date" : "2017-05-30" }];
let newArray = array.map(_=>({x:_.amount,y:_.date}));
console.log(newArray);
You can try using lodash map Lodash map
It will be something like:
const array2 = _.map(array1, (item) => { x: item.amount, y: item.date });