How to transform an indexed array into an associative array? [duplicate] - javascript

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);

Related

Constructing an array of object with two arrays [duplicate]

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 ?

Create a new array from array of objects [duplicate]

This question already has answers here:
Map array by nested array object property, to an array of strings
(4 answers)
Closed 1 year ago.
I am learning javascript.
I have an array that looks like :
myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
I want to create a new array with ["Value1","Value2"].
How should I do that ?
Thanks a lot !
You can use the Javascript function map() for this. :
const myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
const r = myArray.map(e => e.item.fields.myfield)
console.log(r)

JavaScript: convert an array of string to an array of objects [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 2 years ago.
I have an array of strings: const names = ['name1', 'name2', name3']
And I need the following array of objects:
const newArray = [{ name: 'name1' }, { name: 'name2' }, { name: 'name3' }]
How can I create newArray from names, or even possibly convert names itself without creating brand new array?
This can be easily achieved with the map function.
const newArray = names.map((name) => {
return {
name
};
});
map() can do that for you:
const names = ['name1', 'name2', 'name3'];
const newArray = names.map(x=>{return{name:x};});
console.log(newArray);

Filtering specific values on json array and create another array [duplicate]

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();

Convert a Javascript array to a JSON object [duplicate]

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);

Categories

Resources