Convert object to array of objects in JavaScript - javascript

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

Related

best way to search and delete with given values, for delete from array inside the object and return new array of object javascript?

I have tried a couple of methods using findIndex, map, Object.entires
someone help me find the best solution?
**
remove 2 from customerNumber array [1,2,3,4,5]
set to customerNumber value with array[1,3,4,5]
and spread customerNumber to filterState array
**
let filterState = [
{'customerNumber': [1,2,3,4,5]},
{'ward': [10, 20, 30, 40, 50]},
{'enrolledDate': [111, 222,333, 444,555]},
{'district': ['AAA', 'BBB','CCCC', 'DDD']},
{'onBoardingSmsStatus': false}
]
search and delete 2 from customerNumber//customerNumber : 2
function removedChip(type='', value=0, filterState=[]) {
for(let i=0; i<filterState.length; i++) {
let entries = Object.keys(filterState)
.forEach(function eachKey(key) {
console.log(key); // alerts key
console.log(filterState[key]); // alerts value
});
console.log(entries)
let searchIndex = entries.findIndex(type);
console.log('searchIndex', searchIndex)
console.log('type of ', filterState[searchIndex])
for(let i=0; i<filterState[searchIndex]; i++) {
//remove 2 from customerNumber array [1,2,3,4,5]
// set to customerNumber value with array[1,3,4,5]
// and spread customerNumber to filterState array
}
}
}
function invoking with values
removedChip('customerNumber', 10, filterState)
the expected output is
let filterState = [
{'customerNumber': [1,3,4,5]},
{'ward': [10, 20, 30, 40, 50]},
{'enrolledDate': [111, 222,333, 444,555]},
{'district': ['AAA', 'BBB','CCCC', 'DDD']},
{'onBoardingSmsStatus': false}
]
This might help :
function removedChip(type='', value=0, filterState=[]) {
const filterStateTypeArray = filterState.filter(fs =>
Object.keys(fs)[0] === type);
const filterStateTypeItem = filterStateTypeArray ?
filterStateTypeArray[0] : null;
if(!filterStateTypeItem){return;}
let valueArray = filterStateTypeItem[type];
valueArray = valueArray.filter(vA => vA !== value);
filterStateTypeItem[type] = valueArray;
console.log(filterState);
}
let filterState = [
{'customerNumber': [1,2,3,4,5]},
{'ward': [10, 20, 30, 40, 50]},
{'enrolledDate': [111, 222,333, 444,555]},
{'district': ['AAA', 'BBB','CCCC', 'DDD']},
{'onBoardingSmsStatus': false}
]
removedChip('customerNumber', 2, filterState);
Not much of a change from other answers which are all feasible - I'd just split out the functions in 2 to have the filtering handled for an array which can then be tested independently of the parent function or independently from whatever list of objects is inputted
I.e you can have a generic filtering method that can be tested in isolation from the input list of objects.
let filterState = [
{ customerNumber: [1, 2, 3, 4, 5] },
{ ward: [10, 20, 30, 40, 50] },
{ enrolledDate: [111, 222, 333, 444, 555] },
{ district: ['AAA', 'BBB', 'CCCC', 'DDD'] },
{ onBoardingSmsStatus: false },
];
// Independent testable filtering
const removeChipFromArray = (array, removeValue = 0) => {
return array.filter(e => e !== removeValue);
};
// Function to handle the removal of any value from any list of objects
const removeChips = (listOfObjects, value) => {
listOfObjects.forEach((element, index) => {
const key = Object.keys(element);
// General type checker (which would be easier in TS than JS but here is a half-safe way of ensuring you're operating against a list
// You could also convert it to an Array if you think that's better
if (typeof(element[key]) === 'object') {
element[key] = removeChipFromArray(element[key], value);
}
});
};
removeChips(filterState, 2);
console.log(filterState);
In your removedChip You can filter it like..
function removedChip(type = "", value = 0, filterState = []) {
const result = filterState.map((data) => {
if (data[type]) {
// Modify only the given field in the type params
return { [type]: data[type].filter((du) => du !== value) };
}
return data;
});
return result;
}
let filterState = [
{ customerNumber: [1, 2, 3, 4, 5] },
{ ward: [10, 20, 30, 40, 50] },
{ enrolledDate: [111, 222, 333, 444, 555] },
{ district: ["AAA", "BBB", "CCCC", "DDD"] },
{ onBoardingSmsStatus: false }
];
console.log(removedChip("customerNumber", 2, filterState));

How to get a key of a value from a java script Object? [duplicate]

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

if/else statement in map function?

I am here want to use map function in javascript to loop a type data array,but i get error for these syntax below :
function porti(scores) {
const test = scores.map(pass, fail) => {
if (scores < 75){
test.fail
} else {
test.pass
}
return {pass, fail}
}
}
output must be, if scores < 75 : fail, else : pass
console.log(porti([80, 45, 90, 65, 74, 100, 85, 30]));
// { pass: [ 80, 90, 100, 85 ], fail: [ 45, 65, 74, 30 ] }
console.log(porti([]));
// { pass: [], fail: [] }
I think reduce would be better for this situation. This will allow us to reduce the array to an object of two item arrays.
let items = [80, 45, 90, 65, 74, 100, 85, 30]
let result = items.reduce((obj, item) => {
item < 75 ? obj.fail.push(item) : obj.pass.push(item)
return obj
}, {pass:[], fail:[]})
console.log(result)
If you wanted to use filter you could...
let items = [80, 45, 90, 65, 74, 100, 85, 30]
let result = {
pass: items.filter(i => i >= 75),
fail: items.filter(i => i < 75)
}
console.log(result)
And here is how we can do it with forEach...
let items = [80, 45, 90, 65, 74, 100, 85, 30]
let result = {pass:[], fail:[]}
items.forEach(itm => itm < 75 ? result.fail.push(itm) : result.pass.push(itm))
console.log(result)
You could integrate the check as ternary for getting the key for pushing.
function porti(scores) {
var result = { pass: [], fail: [] },
score;
for (score of scores) {
result[score < 75 ? 'fail': 'pass'].push(score);
}
return result
}
console.log(porti([80, 45, 90, 65, 74, 100, 85, 30]));
console.log(porti([]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
As mentioned above .map() should best be saved for when you are looking to return an array by manipulating a previous array. If you don't wish to use a vanilla for loop. You could try this
const testScores = [...someArray of numbers]
function porti(tesScores) {
const result = {
pass: [],
fail: []
}
for (let score of testScores) {
if (score < 75) {
result.fail.push(score)
} else {
result.pass.push(score)
}
return result
}}

Accessing a JSON integer element

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

calculate average result from multi-dimensionally sorted array (JavaScript)

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

Categories

Resources