Convert a Javascript array to a JSON object [duplicate] - javascript

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

Related

Filter if value inside array exists has key in object [duplicate]

This question already has answers here:
Get value from object using 'Array Path'
(3 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 months ago.
array = ['data', 'category', 'hour'];
object = {
"status": {
"type": "INFO",
"messages": []
},
"data": {
"id": 1,
"tenant": "675832",
"process": "6911d872-35f8-11ea-8697-001dd8b71c20",
"category": "resquests"
"time": {
hour: "12",
minute: "30"
}
}
I need to check if object has keys with same value contained in array.
I tried split array by dot, and then filter both array and object but it fails.
const array = inputValue.split('.').map((item) => item);
Do you need something like that?
console.log(arrayEquals(Object.keys(object),array)
with
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}

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

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

Array of flat objects, extract values of a all properties into single object of arrays [duplicate]

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

Calling JSON object with number name? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 3 years ago.
Ex:
{
"123": [
...stuff
]
}
How do I call "123" in a jquery without returning an error?
I've tried:
$.getJSON('insert-url-here').then(function (data) {
console.log(data.123.length)
}
but it doesn't work.
Use square bracket notation:
const data = {
"123": [1, 2, 3]
}
console.log(data[123].length);
Or since it's technically a string:
const data = {
"123": [1, 2, 3]
}
console.log(data["123"].length);

Filter JSON based on some values in javascript [duplicate]

This question already has answers here:
Filter an array based on an object property [duplicate]
(2 answers)
Closed 4 years ago.
I want to filter my JSON in such a way that I only want to have data with "category": 'two'.
Data:
var json = [
{"id": 1,
"category":'one';
}
{"id": 1,
"category":'two';
}
{"id": 1,
"category":'two';
}
{"id": 1,
"category":'three';
}
]
The data is not valid. Objects inside an array and each key-value pair should be separated by comma (,). Correct that then use filter():
var json = [{"id": 1,
"category":'one'
},{"id": 1,
"category":'two'
},{"id": 1,
"category":'two'
},{"id": 1,
"category":'three'
}
]
var res = json.filter(i => i.category == 'two');
console.log(res);

Categories

Resources