I have the following array of objects which has an array inside an
array. the values are in the string, I want it to be a number. What is
the best possible solution to do that in JavaScript.
[
{
"key": "America",
"values": [
[
"1469664000000",
"1"
],
[
"1469750400000",
"13"
],
[
"1469836800000",
"2"
],
[
"1469923200000",
"1"
],
[
"1470009600000",
"1"
],
[
"1470096000000",
"1"
]
]
},
{
"key": "India",
"values": [
[
"1469750400000",
"1"
]
]
},
{
"key": "China",
"values": [
[
"1469664000000",
"2"
],
[
"1469836800000",
"1"
],
[
"1470096000000",
"12"
]
]
},
{
"key": "Africa",
"values": [
[
"1470096000000",
"1"
]
]
},
{
"key": "UK",
"values": [
[
"1469664000000",
"3"
],
[
"1469750400000",
"3"
],
[
"1469836800000",
"1"
],
[
"1469923200000",
"2"
],
[
"1470009600000",
"4"
],
[
"1470096000000",
"2"
]
]
}
]
var data = [{"key":"America","values":[["1469664000000","1"],["1469750400000","13"],["1469836800000","2"],["1469923200000","1"],["1470009600000","1"],["1470096000000","1"]]},{"key":"India","values":[["1469750400000","1"]]},{"key":"China","values":[["1469664000000","2"],["1469836800000","1"],["1470096000000","12"]]},{"key":"Africa","values":[["1470096000000","1"]]},{"key":"UK","values":[["1469664000000","3"],["1469750400000","3"],["1469836800000","1"],["1469923200000","2"],["1470009600000","4"],["1470096000000","2"]]}];
data.forEach(function (o) {
o.values.forEach(function (values) {
values.forEach(function (value, i) {
values[i] = parseInt(value, 10);
});
});
});
console.log(JSON.stringify(data, null, 4));
var data = [{"key":"America","values":[["1469664000000","1"],["1469750400000","13"],["1469836800000","2"],["1469923200000","1"],["1470009600000","1"],["1470096000000","1"]]},{"key":"India","values":[["1469750400000","1"]]},{"key":"China","values":[["1469664000000","2"],["1469836800000","1"],["1470096000000","12"]]},{"key":"Africa","values":[["1470096000000","1"]]},{"key":"UK","values":[["1469664000000","3"],["1469750400000","3"],["1469836800000","1"],["1469923200000","2"],["1470009600000","4"],["1470096000000","2"]]}];
data = data.map(function(t){
var newVals = t.values.map(function(vs){
return vs.map(function(v){
return parseInt(v, 10);
});
});
return {'key': t.key, values: newVals};
});
console.log(JSON.stringify(data));
You can use JSON.parse(your json object
or maybe use two forEachs
Related
How to get the data value from the list, the size of the array, the main thing is not through the index, because the order of the arrays can change and I can get specific data from the code === "size". Unfortunately, the structure cannot be changed. It came to mind only through the filter, by index, but it is impossible
The result should be 100 150
https://jsoneditoronline.org/#left=cloud.b10638604c214b189f87747414e06035
[
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
This data structure is terrible, but a quick fix could be something like this
const data = [
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
const size = data.filter(element => element[1].code === 'size')[0][1].list.map(element => element.value)
console.log(size)
i have the following Javascript object.
var data =
{
"type": [
"car",
"bike"
],
"wheels": [
"4",
"2"
],
"open": [
"Jan",
"Jan"
],
"wheel_press": [
"35",
"19"
],
"max-weight": [
"4000",
"8"
],
"transition_plan": [
"",
null
],
"number_of_occurence": [
5696,
976
],
"model": [
"sedan",
"street"
}
I want to filter on any of the object key to get only the corresponding values for that key.
so far after searching stackoverflow still not able to find similiar question.
I have tried using
data.filter(type ==='car')
and the error i am getting is
.filter is not a function
the expected output is
if i filter on type === 'car'
then it should only show
{
"type": [
"car"
],
"wheels": [
"4"
],
"open": [
"Jan"
],
"wheel_press": [
"35"
],
"max-weight": [
"4000"
],
"transition_plan": [
""
],
"number_of_occurence": [
5696
],
"model": [
"sedan"
}
You can write a function that finds the index of the value inside the given property and then uses Object.fromEntries(), Object.keys() and Array.map() to create a new object as followings
var data={type:["car","bike"],wheels:["4","2"],open:["Jan","Jan"],wheel_press:["35","19"],"max-weight":["4000","8"],transition_plan:["",null],number_of_occurence:[5696,976],model:["sedan","street"]};
const getFilteredObject = (property, value) => {
const index = data[property].indexOf(value)
return Object.fromEntries(Object.keys(data).map(key => ([key, data[key][index]])))
}
const filteredObject = getFilteredObject('type','car');
console.log(filteredObject)
I have a array like this->
var jsonResponse = [
{
"name": "abc",
"value": [
{ "label" : "Daily", "value":"Daily"}
]
},
{
"name": "ccc",
"value": [
{ "label" : "Daily", "value":"Daily"}
]
}
]
And I want to convert it to ->
{
"abc" : {
"name": "abc",
"value": [
{ "label" : "Daily", "value":"Daily"}
]
},
"ccc": {
"name": "ccc",
"value": [
{ "label" : "Daily", "value":"Daily"}
]
}
]
Probably I dont want foreach.
We can do partial with Object.assign( arrayDetails, ...jsonResponse);
But how to do object index?
let indexedResult = {};
jsonResponse.map(obj => indexedResult[obj.name] = obj)
console.log(JSON.stringify(indexedResult));
First of all, this question might have been asked several times already, but every thread I found didn't help me. I would appreciate if one could rewrite my function below.
I have the following array with Json objects.
The goal is to get the "url" values. Right now I get every value from that array.
a = [{
"result": [{
"name": [
"name"
],
"url": [
"www.bar.com"
]
},
{
"name": [
"name 2"
],
"url": [
"www.bar.org"
]
},
{
"name": [
"name 1"
],
"url": [
"www.bar.biz"
]
},
{
"name": [
"name 3"
],
"url": [
"www.bar.jp"
]
}
]
}];
document.getElementById("foo").innerHTML = "How to explicitly get the url value?'";
loopThrough(a);
function loopThrough(obj) {
for (var key in obj) {
// skip loop if the property is from prototype
if (!obj.hasOwnProperty(key)) continue;
if (typeof obj[key] !== 'object') {
console.log(obj[key]);
} else {
loopThrough(obj[key]);
}
}
}
<div id="foo">
</div>
How would I access each "url" element in this array?
Maybe there is also a smarter solution than this.
Thank you in advance.
a = [{
"result": [{
"name": [
"name"
],
"url": [
"www.bar.com"
]
},
{
"name": [
"name 2"
],
"url": [
"www.bar.org"
]
},
{
"name": [
"name 1"
],
"url": [
"www.bar.biz"
]
},
{
"name": [
"name 3"
],
"url": [
"www.bar.jp"
]
}
]
}];
loopThrough(a);
// Uses ES6 syntax
function loopThrough(obj) {
obj[0].result.forEach(r => console.log(r.url[0]));
}
try it yourself.
var a = [{
"result": [{
"name": [
"name"
],
"url": [
"www.bar.com"
]
},
{
"name": [
"name 2"
],
"url": [
"www.bar.org"
]
},
{
"name": [
"name 1"
],
"url": [
"www.bar.biz"
]
},
{
"name": [
"name 3"
],
"url": [
"www.bar.jp"
]
}
]
}];
var urls=a[0].result.reduce(function(urls,it){
return urls.concat(it.url);
},[]);
console.log(urls);
I am converting some data in order to send it to the frontend in a readable way.
I just convert some XML data into JSON, and I am getting an object, but every value is key : ['value'], and I need it key: 'value'
I am sending the data like this res.status(200).json({dealersData}); where dealersData returns this
{
"dealersData": [
{
"DealerId": [
"1"
],
"DealerName": [
"Carmen"
],
"NickName": [
"Carmen"
],
"Picture": [
"00001.jpg"
],
"Active": [
"1"
],
"LegalId": [
"111111111"
],
"TypeId": [
"1"
]
},
{
"DealerId": [
"14"
],
"DealerName": [
"Jaz"
],
"NickName": [
"Jaz"
],
"Active": [
"1"
],
"LegalId": [
"111111187"
],
"TypeId": [
"1"
]
}
]
}
so as you can see I am getting the data "TypeId":["1"] and so on, and I don't want like that, I want "TypeId" : 1, so what should I do to translate that arrays into objects hopefully using Lodash ?
For each dealer iterate over the keys and replace the value with value[0].
dealersData = dealersData.map( function( dealer ){
Object.keys( dealer ).forEach( function(key){
dealer[key] = dealer[key][0];
})
return dealer;
});
http://jsfiddle.net/r38u4qag/1/ (or ES2015 fat arrow version: http://jsfiddle.net/r38u4qag/2/)
This will recursively step through the object, changing arrays of length one to strings:
function process(obj) {
for(var key in obj) {
if(obj[key] instanceof Array && obj[key].length==1) {
obj[key]= obj[key][0];
}
else if(obj[key] instanceof Object) {
process(obj[key]);
}
}
} //process
function process(obj) {
for(var key in obj) {
if(obj[key] instanceof Array && obj[key].length==1) {
obj[key]= obj[key][0];
}
else if(obj[key] instanceof Object) {
process(obj[key]);
}
}
} //process
var obj= {
"dealersData": [
{
"DealerId": [
"1"
],
"DealerName": [
"Carmen"
],
"NickName": [
"Carmen"
],
"Picture": [
"00001.jpg"
],
"Active": [
"1"
],
"LegalId": [
"111111111"
],
"TypeId": [
"1"
]
},
{
"DealerId": [
"14"
],
"DealerName": [
"Jaz"
],
"NickName": [
"Jaz"
],
"Active": [
"1"
],
"LegalId": [
"111111187"
],
"TypeId": [
"1"
]
}
]
}
process(obj);
document.querySelector('pre').innerHTML= JSON.stringify(obj,'\n',2);
<pre></pre>
If I get it right, you have arrays, but you don't need them, since you will always return just a single value, right?
If that's the case, you just have to call .toString() method on your arrays object. I would iterate over your dealersData to do that on all arrays:
for(var i = 0; i < dealersData.length; i++) {
dealersData[i].DealerId = dealersData[i].DealerId.toString();
dealersData[i].DealerName = dealersData[i].DealerName.toString();
[...]
}