Creating a JavaScript Object from JSON File [duplicate] - javascript

This question already has answers here:
Convert array of object to single object in javascript(Angularjs)
(3 answers)
How to convert an array of objects to object with key value pairs
(7 answers)
Reduce array of objects into object [duplicate]
(3 answers)
How to restructure form json data?
(2 answers)
Closed 5 years ago.
I have a json file like this below:
data.json:
[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]
Now, i want to turn this json into javascript object like this format:
JavaScript Object:
{
key0: val0,
key1: val1,
key2: val2,
key3: val3
}
I have tried to find a solution with the functions (like each, map, ...), but i couldn't find a solution. I am not just looking for a solution, but the most efficent one.
Do you have an idea?

reduce is a good option here, as were reducing from an array to an object.
var json = `[
{
"id": 0,
"key": "key0",
"value": "val0"
},
{
"id": 1,
"key": "key1",
"value": "val1"
},
{
"id": 2,
"key": "key2",
"value": "val2"
},
{
"id": 3,
"key": "key3",
"value": "val3"
}
]`;
var obj = JSON.parse(json).reduce((a,v) => { a[v.key] = v.value; return a; }, {});
console.log(obj);

Related

Javascript - convert array to stringify format [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Serializing an object to JSON
(4 answers)
Closed 4 months ago.
I have this array:
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
And i need to convert it to this stringified format:
"{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
I have made several attempts without luck.
My last attempt was this one:
array = array.map((p) => {
return Object.values(p).join(':').replace(/\[/g, '{').replace(/]/g, '}').toString();
}),
Any solutions?
Simply make a empty object, iterate over your array to populate that object, then JSON.stringify(obj) to get your result.
Like this:-
var obj = {};
array = [
{
"name": "name",
"value": "Olá"
},
{
"name": "age",
"value": "23"
},
{
"name": "isVisible",
"value": "1"
}
]
for(let i of array) {
obj[i.name] = i.value;
}
const str = JSON.stringify(obj);
console.log(str);
/*
output : "{\"name\":\"Olá\",\"age\":123,\"isVisible\":true}"
*/

From Json To array javascript with exists items [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I need to convert JSON object to javascript array. This my object
var object =
[
{
"Key": "2019",
"value": 3
},
{
"Key": "2020",
"value": 3
},
{
"Key": "2021",
"value": 3
}
]
i need to weel be my array like this ['2019','2020','2021']
i try to to this
var data_t = [];
for (var i in object ) {
data_t.push(object );
}
console.log(data_t[0]);
But not working !!
You can use .map() for this:
var object = [
{
"Key": "2019",
"value": 3
},
{
"Key": "2020",
"value": 3
},
{
"Key": "2021",
"value": 3
}
];
console.log(object.map(a => a.Key));
var data_t = [];
for (var i in object ) {
data_t.push(i.Key);
}

Data Manipulation of json encoded string [duplicate]

This question already has answers here:
Pivot or Transforming JavaScript object
(2 answers)
Closed 3 years ago.
I have the following JSON encoded string that I would like to manipulate using javascript such that the x and y axis values are combined into a two dimensional array names values.
[
{
"key": "0",
"xaxis": "1492041600000",
"yaxis": "512"
},
{
"key": "0",
"xaxis": "1492045200000",
"yaxis": "985"
},
{
"key": "1",
"xaxis": "1492048800000",
"yaxis": "685"
},
{
"key": "1",
"xaxis": "1492052400000",
"yaxis": "935"
}
]
I needs to render as:
[
{
"key": "0",
"values": [
[1492041600000, 512],
[1492045200000, 985]
]
},
{
"key": "1",
"values": [
[1492048800000, 685],
[1492052400000, 935]
]
}
]
Can someone please show me how to perform this data manipulation?
Thank you!
Using map-reduce, you should be able to get the behavior you want.
const results = [{"key":"0","xaxis":"1492041600000","yaxis":"512"},{"key":"0","xaxis":"1492045200000","yaxis":"985"},{"key":"1","xaxis":"1492048800000","yaxis":"685"},{"key":"1","xaxis":"1492052400000","yaxis":"935"}]
const reducedResults = results.reduce((acc, result) => {
if(acc[result.key]) {
acc[result.key].values.push([result.xaxis, result.yaxis]);
} else {
acc[result.key] = { key: result.key, values: [[result.xaxis, result.yaxis]] }
}
return acc;
}, {});
const newResults = Object.values(reducedResults);
console.log(newResults);

Sort an array of objects based on a numeric key given as String values [duplicate]

This question already has answers here:
Why doesn't this arrow function work in IE 11?
(5 answers)
Javascript: Converting String to Number?
(4 answers)
Sort array of objects by string property value
(57 answers)
Closed 4 years ago.
I have an array with key-value pairs, array columns are id and name. I want to sort this array by id.
The id column value is of type string type but I want to sort them as numeric values also should work on IE
Here is my code:
var items = [{
"id": "165",
"name": "a"
},
{
"id": "236",
"name": "c"
},
{
"id": "376",
"name": "b"
},
{
"id": "253",
"name": "f"
},
{
"id": "235",
"name": "e"
},
{
"id": "24",
"name": "d"
},
{
"id": "26",
"name": "d"
}
];
console.log(items.sort((a, b) => Number(a.ID) - Number(b.ID)))
Though the order does change it doesn't change as expected also in IE an error is thrown.
Now you can do it with pure JS, using the sort method..
var items = [
{
"id": "165",
"name": "a"
},
{
"id": "236",
"name": "c"
},
{
"id": "376",
"name": "b"
},
{
"id": "253",
"name": "f"
},
{
"id": "235",
"name": "e"
},
{
"id": "24",
"name": "d"
},
{
"id": "26",
"name": "d"
}
];
items.sort((a,b)=>+a.id>+b.id);
console.log(items);

Create new array using JSON results with javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Hello I have the following JSON result. Its a small snippet from a large JSON file. Theres hundreds of entries.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
I would like a way to loop through this data using javascript to create a new array stored in a variable that pulls just the ids. So the new array would look like this:
data = [1, 2, 3];
If theres 1000 entries, then the array would go from 1-1000.
Thanks!
Very simple just use array.map.
var jsonResult = [
{
"id": "1",
"name": "one",
},
{
"id": "2",
"name": "two",
},
{
"id": "3",
"name": "three",
}
]
let idArray = jsonResult.map(element => element["id"]);
console.log(idArray)

Categories