I have an JSON-Object as follows:
Input for the months is
customerSend,customerReceived,totalSendAllCustomers,totalReceivedAllCustomers
var emailObj = {
"kundenNummer":17889,
"jahre":
{
2017:{
"Januar":[15,30,75,125],
"Februar":[17,32,77,127],
"März":[19,34,79,129],
},
2018:{
"Januar":[28,12,66,198],
"Oktober":[40,4,40,5],
}
}
}
How exactly do I access the specific year?
I already tried it like this:
var keysYears = Object.keys(emailObj.jahre);
var currentSelectedYear = keysYears[0];
var keysMonth = Object.keys(emailObj.jahre[currentSelectedYear]);
var currentSelectedMonth = keysMonth[0];
document.write(emailObj.jahre[currentSelectedYear].2017[0]);
I also tried some other ways of doing this but I already deleted those.
Can you tell me how to access the 2017 or 2018 data?
I know that I could convert them into strings but I want to know if I could also do it this way.
You can call the properties of your object emailObj by their names.
Either with a dot notation
emailObj.kundenNummer
Or by brackets notation
emailObj["kundenNummer"]
The dot notation won't work in your case because the name of your property is a number. You should then use
emailObj.jahre["2017"]
var emailObj = {
"kundenNummer": 17889,
"jahre": {
"2017": {
"Januar": [15, 30, 75, 125],
"Februar": [17, 32, 77, 127],
"März": [19, 34, 79, 129],
},
"2018": {
"Januar": [28, 12, 66, 198],
"Oktober": [40, 4, 40, 5],
}
}
};
let year = "2017";
let month = "Januar";
console.log(emailObj.jahre[year][month]);
You should use bracket notation.
document.write(emailObj.jahre[currentSelectedYear][currentSelectedMonth][0]);
var emailObj = {
"kundenNummer":17889,
"jahre":
{
2017:{
"Januar":[15,30,75,125],
"Februar":[17,32,77,127],
"März":[19,34,79,129],
},
2018:{
"Januar":[28,12,66,198],
"Oktober":[40,4,40,5],
}
}
}
var keysYears = Object.keys(emailObj.jahre);
var currentSelectedYear = keysYears[0];
var keysMonth = Object.keys(emailObj.jahre[currentSelectedYear]);
var currentSelectedMonth = keysMonth[0];
document.write(emailObj.jahre[currentSelectedYear][currentSelectedMonth][0]);
In a JavaScript object, the key is always a string, even if you use an integer it will be converted into a string.
obj = {
key1: //contents
key2: //contents
}
To access a specific key:
obj.key1
obj['key1']
For your example:
emailObj.jahre['2017']
emailObj['jahre']['2017']
Use the for in looping construct to loop through the keys of an object:
var emailObj = {
"kundenNummer":17889,
"jahre": {
2017:{
"Januar":[15,30,75,125],
"Februar":[17,32,77,127],
"März":[19,34,79,129],
},
2018:{
"Januar":[28,12,66,198],
"Oktober":[40,4,40,5],
}
}
}
for (key in emailObj.jahre) {
console.log(emailObj.jahre[key]) //Here key will be '2017', '2018' etc
}
You cannot access with dot notation properties which contain as name a number in JavaScript. Instead you should consider using bracket notation.
Example:
emailObj.jahre['2017']
var emailObj = {
"kundenNummer": 17889,
"jahre": {
2017: {
"Januar": [15, 30, 75, 125],
"Februar": [17, 32, 77, 127],
"März": [19, 34, 79, 129],
},
2018: {
"Januar": [28, 12, 66, 198],
"Oktober": [40, 4, 40, 5],
}
}
};
console.log(emailObj['jahre']['2017']);
console.log(emailObj.jahre['2017']);
Related
I'm trying to sum the indices of an array and save them into a new one.
I have included an example array below, as well as my first try to solve this:
var data = [
["29.01.2020", 40, 10],
["30.01.2020", 80, 100],
["31.01.2020", 90, 500],
["01.02.2020", 30, 205],
["02.02.2020", 10, 74],
["03.02.2020", 30, 120]
];
The results I'm trying to create are something like these (either an array or an object):
[['0120', 210, 610], ['0220', 70, 399]]
{
"0120": {
"abc": 210,
"def": 610
},
"0220": {
"abc": 70,
"def": 399
}
}
This is something I found and was playing around with, perhaps something that might work with a little editing:
var res = {};
data.forEach((item)=>{
if(Object.keys(res).includes(item[0])){
res[item[1]] += item[1];
} else {
res[item[0]] = item[1];
}
});
Shortening the first index of my data array should be possible by using substrings I guess? What I'm mainly struggling with is getting it to summarize the index.
Any ideas how I can get there?
Thanks in advance
First, you can get the key from the first element of each subarray using String.split function. Using split function, you can extract the month and year string and combine it to generate the key of each array.
And based on that key, using Array.prototype.reduce, you can generate the content you want.
const data = [
["29.01.2020", 40, 10],
["30.01.2020", 80, 100],
["31.01.2020", 90, 500],
["01.02.2020", 30, 205],
["02.02.2020", 10, 74],
["03.02.2020", 30, 120]
];
const result = data.reduce((acc, cur) => {
const dateArr = cur[0].split('.');
const key = dateArr[1] + dateArr[2].slice(-2);
if (acc[key]) {
acc[key].abc += cur[1];
acc[key].def += cur[2];
} else {
acc[key] = {
abc: cur[1],
def: cur[2]
};
}
return acc;
}, {});
console.log(result);
You can use array reduce method. Split the date string by dot. Then make it a key(according to your requirement two digit from month and last two digit from year) and based on that key just count it. At last using Object.values output the array.
const data = [
['29.01.2020', 40, 10],
['30.01.2020', 80, 100],
['31.01.2020', 90, 500],
['01.02.2020', 30, 205],
['02.02.2020', 10, 74],
['03.02.2020', 30, 120],
];
const ret = data.reduce((prev, c) => {
const p = prev;
const date = c[0].split('.');
const key = `${date[1]}${date[2].slice(-2)}`;
if (!p[key]) p[key] = [key, c[1], c[2]];
else p[key] = [key, p[key][1] + c[1], p[key][2] + c[2]];
return p;
}, {});
console.log(Object.values(ret));
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
Following is an example of js object:
var arr =
{
"a1": { "0.25": 13, "0.5": 50},
"a2": { "0.5": 50, "0.75": 113, "1": 202}
}
id = 'a1';
key = "0.25";
function myFunc(id, key) {
return arr[id][key];
}
Using the above function I can get the value of the corresponding key.
My question how can I get the Key for a given value?
E.g. if id = 'a2' and value = 113, the function should return the corresponding key 0.75
You can use Object.entries to get the key
var arr = {
"a1": {
"0.25": 13,
"0.5": 50
},
"a2": {
"0.5": 50,
"0.75": 113,
"1": 202
}
}
function myFunc(a, key) {
var k = Object.entries(arr[a]).flat();
return k[k.indexOf(key) - 1]
}
console.log(myFunc('a1', 13))
You can get keys for arr[a] first and than filter key based on value
var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}
let a = 'a1',val = 13;
let getKey = (a,val) => Object.keys(arr[a]).filter(e => arr[a][e] === val)
console.log(getKey(a,val))
console.log(getKey('a2',113))
If you're sure that there's always one key with matching value or you want the first matching values key only than you can use find instead of filter
var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}
let a = 'a1',val = 13;
let getKey = (a,val) => Object.keys(arr[a]).find(e => arr[a][e] === val)
console.log(getKey(a,val))
console.log(getKey('a2',113))
Use Object.entries() and Array#find
var arr ={"a1": { "0.25": 13, "0.5": 50},"a2": { "0.5": 50, "0.75": 113, "1": 202}}
var a = 'a2',val = 113;
var Key = (a,val) => Object.entries(arr[a]).find(i=> i[1] == val)[0];
console.log(Key(a,val))
I have some data passed in via a query string and I've converted it to an object like:
{
"Person1_Age": 22,
"Person1_Height": 170,
"Person1_Weight": 72,
"Person2_Age": 27,
"Person2_Height": 160,
"Person2_Weight": 56,
}
I want to convert this to an array of objects like this:
[
{
"name": "Person1",
"age": "22",
"height": 170,
"weight": 72
},
{
"name": "Person2",
"age": "27",
"height": 160,
"weight": 56
}
]
What would be the best way to do this? Thanks!
You could do this with forEach loop and optional thisArg parameter
var data = {
"Person1_Age": 22,
"Person1_Height": 170,
"Person1_Weight": 72,
"Person2_Age": 27,
"Person2_Height": 160,
"Person2_Weight": 56,
}
var result = [];
Object.keys(data).forEach(function(e) {
var part = e.split('_');
var person = part[0];
var p = part[1].toLowerCase();
if(!this[person]) {
this[person] = {name: person}
result.push(this[person]);
}
this[person][p] = data[e];
}, {})
console.log(result);
Here's one way to do it
var obj = {
"Person1_Age": 22,
"Person1_Height": 170,
"Person1_Weight": 72,
"Person2_Age": 27,
"Person2_Height": 160,
"Person2_Weight": 56,
}
var map = {}, arr = [];
Object.keys(obj).forEach(function(k) {
var parts = k.split('_'),
key = parts.shift().toLowerCase(),
val = parts.pop().toLowerCase();
if (!(key in map)) map[key] = {};
map[key][val] = obj[k];
});
for (var k in map) {
map[k].name = k;
arr.push(map[k]);
}
document.body.innerHTML = '<pre>' + JSON.stringify(arr, 0, 4) + '</pre>';
You could use a hash table for reference and push the object to the Array, if a new hash is found.
var obj = { Person1_Age: 22, Person1_Height: 170, Person1_Weight: 72, Person2_Age: 27, Person2_Height: 160, Person2_Weight: 56 },
arr = [];
Object.keys(obj).forEach(function(k) {
var parts = k.split('_');
if (!this[parts[0]]) {
this[parts[0]] = { name: parts[0] };
arr.push(this[parts[0]]);
}
this[parts[0]][parts[1].toLowerCase()] = obj[k];
}, Object.create(null));
console.log(arr);
Just for the record, this can also be done directly as the result of a .reduce() method.
var list = {
"Person1_Age": 22,
"Person1_Height": 170,
"Person1_Weight": 72,
"Person2_Age": 27,
"Person2_Height": 160,
"Person2_Weight": 56
};
var res = Object.keys(list).reduce(function(a, b) {
var m = b.match(/Person(\d+)_(.+)/);
(a[m[1] - 1] = a[m[1] - 1] || {name: 'Person' + m[1]})[m[2].toLowerCase()] = list[b];
return a;
}, []);
console.log(res);
Below is the layout of my JSON File.
{
"questions": ["Question1", "Question2"],
"orgs": ["Org1", "Org2", "Org3"],
"dates": ["Q1", "Q2", "Q3"],
"values": [
[
[5, 88, 18],
[50, 83, 10],
[29, 78, 80]
],
[
[46, 51, 61],
[95, 21, 15],
[49, 86, 43]
]
]
}
I'm trying to retrieve a single array of values by looping through each question, indexed by an "orgs" value and then adding each value retrieved and dividing it by data.dates.length.
Here is my code;
d3.json("data.json", function(error, data) {
var array = new Array()
var orgS = "Org2"
var org = data.orgs.indexOf(orgS);
for (var question = 0; question < data.questions.length; question++) {
array.push(
data.values[question][org]
)
console.log(array)
}
// add array together
array.reduce(function(a, b) {
return a + b;
})
// calculate average
var avg = array / data.dates.length;
})
Here is a plnk;
http://plnkr.co/edit/wMv8GmkD1ynjo9WZVlMb?p=preview
I think the issue here is how I'm retrieving the values in the first place? as at the moment, although I am retrieving the correct values in the console log, I'm getting the array twice, and both times inside nested arrays. I'm not so sure how to remedy the problem?
For reference;
[question1][org1] corresponds to the values [5, 88, 18].
Hope someone can offer some advice here?
Thanks!
Since you clarified your question to indicate you want to calculate separate averages for each question, I've rewritten my answer. You should do all the calculations in the for loop, since the loop is looping through the questions. Then store your averages in an array.
d3.json("data.json", function(error, data) {
var averages = new Array()
var orgS = "Org2"
var org = data.orgs.indexOf(orgS);
var values, sum;
for (var question = 0; question < data.questions.length; question++) {
// get the values for the question/org
values = data.values[question][org];
// calculate the sum
sum = values.reduce(function(a, b) {
return a + b;
});
// calculate the average
averages.push(sum / values.length);
}
console.log(averages);
});
Perform the .reduce() in the for loop and push that result into array. That will give you the an array of the results you expected.
array.push(data.values[question][org].reduce(function(a, b) {
return a + b
}, 0) / data.dates.length)
[
47.666666666666664,
43.666666666666664
]
Currently, you're attempting to perform addition on the arrays themselves in the .reduce() callback instead of reducing the members of each individual array to their sum, and then average.
Demo: (Click the text below to show the whole function)
var data = {
"questions": ["Question1", "Question2"],
"orgs": ["Org1", "Org2", "Org3"],
"dates": ["Q1", "Q2", "Q3"],
"values": [
[
[5, 88, 18],
[50, 83, 10],
[29, 78, 80]
],
[
[46, 51, 61],
[95, 21, 15],
[49, 86, 43]
]
]
}
x(data)
// Your callback function.
function x(data) {
var array = new Array()
var orgS = "Org2"
var org = data.orgs.indexOf(orgS);
for (var question = 0; question < data.questions.length; question++) {
array.push(data.values[question][org].reduce(function(a, b) {
return a + b
}, 0) / data.dates.length)
}
console.log(array)
}
Instead of a for loop, you could also use .map().
var array = data.questions.map(function(_, question) {
return data.values[question][org].reduce(function(a, b) {
return a + b
}, 0) / data.dates.length
})
Demo: (Click the text below to show the whole function)
var data = {
"questions": ["Question1", "Question2"],
"orgs": ["Org1", "Org2", "Org3"],
"dates": ["Q1", "Q2", "Q3"],
"values": [
[
[5, 88, 18],
[50, 83, 10],
[29, 78, 80]
],
[
[46, 51, 61],
[95, 21, 15],
[49, 86, 43]
]
]
}
x(data)
// Your callback function.
function x(data) {
var orgS = "Org2"
var org = data.orgs.indexOf(orgS);
var array = data.questions.map(function(_, question) {
return data.values[question][org].reduce(function(a, b) {
return a + b
}, 0) / data.dates.length
})
console.log(array)
}
You need to store the sum, the result of reduce.
// add array together
// store in sum
var sum = array.reduce(function(a, b) {
return a + b;
}, 0); // use 0 as start value
For the average, you do not need the length of data.dates but from array, because you collecting the values and this length is important.
// calculate average
var avg = sum / array.length;
Together for all values, you might get this
var data = { "questions": ["Question1", "Question2"], "orgs": ["Org1", "Org2", "Org3"], "dates": ["Q1", "Q2", "Q3"], "values": [[[5, 88, 18], [50, 83, 10], [29, 78, 80]], [[46, 51, 61], [95, 21, 15], [49, 86, 43]]] },
sum = [];
data.values.forEach(function (a, i) {
sum[i] = sum[i] || [];
a.forEach(function (b) {
b.forEach(function (c, j) {
sum[i][j] = sum[i][j] || 0;
sum[i][j] += c;
});
});
});
data.avg = sum.map(function (a, i) {
return a.map(function (b) {
return b / data.values[i].length;
});
});
console.log(sum);
console.log(data);
I have a json object {"yAxis" : { "Batting" : [10, 20, 30, 40] , "Bowling" : [10, 30, 50, 70], "Fielding" : [20, 40, 50, 70] } },
and want to convert to json array like this
[ { "name": "Batting" ,"data": [10,20,30,40]} , { "name": "Bowling" ,"data": [10,30,50,70] },{ "name": "Fielding" ,"data": [20,40,50,70]}]
I can create json object for each index but how can i put those to json array?
for(var abc in objJSON.yAxis)
{
seriesValues += JSON.stringify({name:abc,data:(objJSON.yAxis)[abc]});
}
Any help?
You can just loop through creating the object, don't worry about stringifying it.
var obj = {"yAxis" : { "Batting" : [10, 20, 30, 40] , "Bowling" : [10, 30, 50, 70], "Fielding" : [20, 40, 50, 70] } };
var keys = Object.keys(obj.yAxis),
narray = [];
keys.forEach(function(v) {
narray.push({ "name": v, "data": obj.yAxis[v] });
});
Then, if you need to stringify it, you can simply do a JSON.stringify(narray) later on.
You're close:
var seriesValues = [];
for (var abc in objJSON.yAxis) {
seriesValues.push({name: abc, data: objJSON.yAxis[abc]});
}
That will make a javascript array. If you need it as a JSON string then append:
var myString = JSON.stringify(seriesValues);
Simply stringify after the array generated all around.
var result = [];
for(var name in objJSON.yAxis)
{
result.push({name: name, data: objJSON.yAxis[name]});
}
return JSON.stringify(result);