i want to read the value "name" or "count".
"facet_groups": [
{
"facets": [
{
"count": 233,
"path": "75011",
"state": "displayed",
"name": "75011"
},
{
"count": 180,
"path": "75015",
"state": "displayed",
"name": "75015"
},
This is my code :
LoadData(request)
.then(function(response){
console.log(response);
let facet = response.facet_groups;
for(let i=0; i<50; i++){
let arrondisement = facet[i].facets[i].name;
console.log(arrondisement);
}
When i try this i have this message : "OpenDataParis.js:43 TypeError: Cannot read property 'facets' of undefined"
it need multiple loop and to avoid error it better to use array.length instead fixed number.
let response = {
"facet_groups": [{
"facets": [{
"count": 233,
"path": "75011",
"state": "displayed",
"name": "75011"
},
{
"count": 180,
"path": "75015",
"state": "displayed",
"name": "75015"
}
]
}]
}
let facet_groups = response.facet_groups;
for (let i = 0; i < facet_groups.length; i++) {
for (let j = 0; j < facet_groups[i].facets.length; j++) {
let name = facet_groups[i].facets[j].name;
let count = facet_groups[i].facets[j].count;
console.log("for:", name, count);
}
}
facet_groups.forEach(function(item) {
item.facets.forEach(function(facet) {
console.log("forEach:", facet.name, facet.count)
})
})
You were missing that your array actually starts within object index[0] and that you need to loop through the facets array not the facet groups array. If you want to loop through both I would nest a couple for loops following this example:
data = {
"facet_groups": [
{
"facets": [
{
"count": 233,
"path": "75011",
"state": "displayed",
"name": "75011"
},
{
"count": 180,
"path": "75015",
"state": "displayed",
"name": "75015"
}
]
}
]
}
let facets = data.facet_groups[0].facets;
let arrondisements = []
for(let i=0; i<facets.length; i++){
arrondisements.push(facets[i].name);
};
console.log(arrondisements);
Related
I have a JSON with both subject fields and their corresponding values separately. I want to write a nested for loop that will assign the value to each subject field from below json. Right now, I am using a single for loop to loop through all the values by defining the subject field. I will be using sqlcmd-insert in each for loop after I assign the values to subjects.
for(var i = 0; i < obj.value.length; i++){
const Name= mm[i][0];
const Age= mm[i][1];
const Country= mm[i][2];
const Gender= mm[i][3];
const IsActive= mm[i][3];
}
I don't want to define variable names, instead I want it to be taken directly from JSON subject field and assign the corresponding value. I tried below nested loop but it doesn't work. How can I make this work? Please help.
for(var i = 0; i < obj.value.length; i++)
{
for(var j = 0; j < obj.subject.length; j++)
{
const obj.subject[i].field = obj.value[i][j];
}
}
{
"First": 1,
"Last": 3,
"msg": "",
"subject": [
{
"col": 0,
"field": "Name"
},
{
"col": 1,
"field": "Age"
},
{
"col": 2,
"field": "Country"
},
{
"col": 3,
"field": "Gender"
},
{
"col": 4,
"field": "IsActive"
}
],
"value": [
[
"Sam",
30,
"US",
"Male",
"Y"
],
[
"Tom",
32,
"UK",
"Male",
"Y"
],
[
"Kate",
28,
"USA",
"Female",
"N"
]
]
}
for(var i = 0; i < obj.value.length; i++)
{
obj.subject[0].name=obj.value[i][0];
}
for(var i = 0; i < obj.value.length; i++)
{
obj.subject[0].field=obj.value[i][0];
obj.subject[0].field=obj.value[i][1];
obj.subject[0].field=obj.value[i][2];
obj.subject[0].field=obj.value[i][3];
obj.subject[0].field=obj.value[i][4];
}
I have this array that needs to be parsed into a useful object. The names of each value are a collection of namespaces separated by / characters. The values between each '/' need to be turned into a JS Objects property:
"status": [
{
"message": "OK",
"name": "/Computer",
"values": []
},
{
"name": "/Computer/CPU Usage",
"values": []
},
{
"name": "/Computer/CPU Temp",
"values": []
},
{
"name": "/Computer/hardware/memory",
"values": []
}
]
I need it to become this:
"status": {
"computer": {
"CPU Usage": {
"values": []
},
"CPU Temp": {
"values": []
},
"hardware": {
"memory": {
"values": []
}
}
}
}
So far I have done this:
var statii = status, // from above..
_parsedStatii = {};
for (var i = 0; statii.length < 0; i ++) {
var _nameSpaces = statii[i].name.split('/');
// Start at 1 because index 0 is empty (before the first slash)
if (!_parsedStatii[_nameSpaces[1]]) {
_parsedStatii[_nameSpaces[1]] = {};
}
if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]])
_parsedStatii[_nameSpaces[1]][_nameSpaces[2]] = {};
if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]])
_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]] = {};
if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]][_nameSpaces[4]])
_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]][_nameSpaces[4]] = {};
}
Obviously it is no where near right, I have tried a lot of recursive functions but am at a bit of a loss. This example gives the clearest representation of what I am trying to achieve. Any ideas? (Please excuse code typos, it was paraphrased)
You could split the name and build an object upon.
var data = { "status": [{ "message": "OK", "name": "/Computer", "values": [] }, { "name": "/Computer/CPU Usage", "values": [] }, { "name": "/Computer/CPU Temp", "values": [] }, { "name": "/Computer/hardware/memory", "values": [] }] },
object = {};
data.status.forEach(function (a) {
a.name.slice(1).split('/').reduce(function (o, k) {
return o[k] = o[k] || {};
}, object).values = a.values;
});
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
My object json is like this :
var a = [{
"attributes": {
"Code": "SGL",
"Total": "811400"
},
"DayPrice": {
"Date": "2016-07-22",
"Rate": "811400"
}
}];
I want change DayPrice to array like this :
var a = [{
"attributes": {
"Code": "SGL",
"Total": "811400"
},
"DayPrice": [{
"Date": "2016-07-22",
"Rate": "811400"
}]
}];
Any solution to solve my problem?
Thank you very much
You would need to loop through the array of objects and wrap the DayPrice property of each in an array, like this:
for (var i = 0; i < a.length; i++) {
a[i].DayPrice = [a[i].DayPrice];
}
Working example
Assign the an array with the property to the property.
a[1].DayPrice = [a[1].DayPrice];
Or use a loop:
var a = [{ "attributes": { "Code": "SGL", "Total": "811400" }, "DayPrice": { "Date": "2016-07-22", "Rate": "811400" } }];
a.forEach(function (a) {
if ('DayPrice' in a) {
a.DayPrice = [a.DayPrice];
}
});
document.write('<pre>' + JSON.stringify(a, 0, 4) + '</pre>');
Hope this will help
var a = [{
"attributes": {
"Code": "SGL",
"Total": "811400"
},
"DayPrice": {
"Date": "2016-07-22",
"Rate": "811400"
}
}];
var _getDayPrice = a[0].DayPrice;
var _dayPriceArray = [];
_dayPriceArray.push({
"Date":_getDayPrice.Date,
"Rate":_getDayPrice.Rate
})
a[0].DayPrice=_dayPriceArray;
console.log(a);
Check this jsFiddle
I have an array of objects, which has an array of 'tracks.' What I would like to do is compare the mbid values across all of the track objects, check if there are any duplicates, and store the duplicate track objects into a new array. Any help or guidance?
[
{
"track": [
{
"name": "Radiapathy",
"mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
"url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
},
{
"name": "How Did I Get Here",
"mbid": "64b3078f-89cd-4ad5-bc7a-b43af082b00f",
"url": "http://www.last.fm/music/Odesza/_/How+Did+I+Get+Here"
},
{
"name": "Sunshine Roof",
"mbid": "837db975-c93e-45ca-992c-0c924ef0f34f",
"url": "http://www.last.fm/music/The+Innocence+Mission/_/Sunshine+Roof"
}
]
},
{
"track": [
{
"name": "Traveling",
"mbid": "b40c24b8-3295-4219-af59-855b69958ca2",
"url": "http://www.last.fm/music/Tennis/_/Traveling"
},
{
"name": "Ghost",
"mbid": "6273ae8f-3d2c-44c6-8c0d-53013ba79b4e",
"url": "http://www.last.fm/music/Neutral+Milk+Hotel/_/Ghost"
},
{
"name": "Strange",
"mbid": "5a015df2-6c4a-4192-bea8-14ec5f297713",
"url": "http://www.last.fm/music/Built+to+Spill/_/Strange"
}
]
},
{
"track": [
{
"name": "Radiapathy",
"mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
"url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
},
{
"name": "Let Me Show You Love",
"mbid": "",
"url": "http://www.last.fm/music/Cut+Copy/_/Let+Me+Show+You+Love"
},
{
"name": "Footsteps",
"mbid": "",
"url": "http://www.last.fm/music/Cut+Copy/_/Footsteps"
}
]
}
]
The answer to this question (Elminating duplicates in a JSON object) pretty much answers yours. I've used this (the "Smart(er) way") and modified to suit your array & requirements:
var key = null;
var noDupes = [];
var dupes = [];
for (var i = 0; i < arr.length; i++)
{
// loop through each track:
for (var j = 0; j < arr[i].track.length; j++)
{
key = arr[i].track[j].mbid;
if (!hash.contains(key))
{
hash.add(key);
noDupes.push(arr[i].track[j]); // if not duplicate
}
else
{
dupes.push(arr[i].track[j]); // if duplicate
}
}
}
http://jsfiddle.net/6wspy/
I have doubt about data structure transformation from one form of array to another.
My input data is of the form,
var testVar=[
{
"count": 1,
"term": "Company",
"Company": [
{
"Tata": [
{
"count": 1,
"term": "Tatagroup"
}
],
"sector": "Automotive",
"type": "Car"
},
]
},
{
"count": 2,
"term": "Country",
"Country": [
{
"France": [
{
"count": 1,
"term": "France"
}
],
"region": "Europe",
"Player": "Zidane",
"term": "France",
"code": "FRA"
},
{
"region": "Europe",
"Player": "Federer",
"term": "Switzerland",
"Switzerland": [
{
"count": 1,
"term": "Switzerland"
}
],
"code": "SWI"
}
]
}];
and I am trying to transform it to the form,
[ "Tata" : [{"sector" : "automotive"}, "type" : "car"], "France": [{"region" : "Europe}, {"Player" : "Zidane"} , {"code" : "FRA"}], "switzerland" : [{"region" : "Europe}, {"Player" : "Federer"} , {"code" : "SWI"}]];
The code I came up with looks like http://jsfiddle.net/X2apw/2/, bt its nt accurate..
var testvar = [...];
var result = {};
for (var i=0; i<testvar.length; i++) {
var arr = testvar[i][testvar[i].term];
for (var j=0; j<arr.length; j++) {
var resarr = [];
for (var k in arr[j]) {
if (typeof arr[j][k] == "string") {
var obj = {};
obj[k] = arr[j][k];
resarr.push(obj);
} else {
result[k] = resarr;
}
}
}
}
(Demo at jsfiddle.net)
However, I strongly recommend to use just one object instead of an array of one-property-objects in your result format. Change the inner loop body to:
var resobj = {};
for (var k in arr[j]) {
if (typeof arr[j][k] == "string") {
resobj[k] = arr[j][k];
} else {
result[k] = resobj;
}
}
(Demo)