Create dynamic object with dynamic keys and non dynamic - javascript

I am trying to create nested objects with some dynamic and not dynamic fields
this is my dataset let method = {
"97": {
"mini_id": 75,
"score": "2",
"fe_id": 97
},
"98": {
"mini_id": 75,
"score": "2",
"fe_id": 98
},
"169": {
"mini_id": 76,
"score": "1",
"fe_id": 169
},
"170": {
"mini_id": 76,
"score": "1",
"fe_id": 170
},
"171": {
"mini_id": 76,
"score": "1",
"fe_id": 171
},
"172": {
"mini_id": 76,
"score": "3",
"fe_id": 172
}
}
And I am trying to have the data in the format of
var reduced = Object.values(arr).reduce((c,v) => {
c[v.mini_id] = c[v.mini_id] || {};
c[v.mini_id]["fees"][v.fe_id] = c[v.mini_id]["fees"][v.fe_id] || {};
return c;
}, {})
I want my dictionary to look like :
{75: {fees: {97: {....}}}}
Looks like it doesn't like dynamic keys to be mixed with non dynamic. Any suggestions?

The following code snippet might help you,
const arr = {
97: {
mini_id: 75,
score: "2",
fe_id: 97,
},
98: {
mini_id: 75,
score: "2",
fe_id: 98,
},
169: {
mini_id: 76,
score: "1",
fe_id: 169,
},
170: {
mini_id: 76,
score: "1",
fe_id: 170,
},
171: {
mini_id: 76,
score: "1",
fe_id: 171,
},
172: {
mini_id: 76,
score: "3",
fe_id: 172,
},
};
const reduced = Object.values(arr).reduce((c, v) => {
c[v.mini_id] = c[v.mini_id] ?? { fees: {} };
c[v.mini_id].fees[v.fe_id] = c[v.mini_id].fees[v.fe_id] ?? {};
return c;
}, {})
console.log(reduced);

Related

Get the index of the largest value of an array of objects

I have an object table in which there is the score and the name of a character and I would like to retrieve the index with the highest score to be able to make a scoreboard.
This is what my array looks like
[
{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"
}
]
In this case, I would like to get the index of the person who has the highest score, in this case, the index is 3 because it is tutu who has the highest score.
Thank advance for ur help
The sort function should do it:
var raw_scores = [
{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"
}
]
var sorted_scores = raw_scores.sort(function(a,b){return b.score - a.score})
More info at w3schools
Using for loop
var index = 0;
var max = 0;
for (var i = 0; i < scores.length; i++) {
if (s[i].score > max) {
max = s[i].score;
index = i;
}
}
console.log(index);
You can use the reduce function
const array = [
{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"
}
];
const highestScore = array.reduce((last, item) => {
// return the item if its score is greater than the highest score found.
if(!last || last.score < item.score) {
return item;
}
return last;
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
var data = [{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"x
}
];
var max_score = Math.max.apply(Math, data.map(function(o) {
return o.score;
}))
console.log(data.filter(i => i.score === max_score))
[...].reduce((acc, item, idx) => (item.score > acc.score ? {score: item.score, index: idx} : acc), {score: 0, index:0}).index

Retrive Value from nested object using recursion

I have an JS object data, from this array of object, I need to take "amount" value which is inside of "limitBreakup" Array. I have done using .map(), But I am intersted to know the implementation of the same using Recursion.
var data = [
{
"limitRequirementId":"123",
"facilityType":"cc",
"amount":800000,
"existingRoi":12,
"existingPf":12100,
"repoRate":5,
"spread":10,
"tenure":24,
"margin":10000,
"loanVariable":{
"type":"roi/commission",
"value":15
},
"limitBreakup":[
{
"limitRequirementId":"13",
"facilityType":"cc",
"repoRate":5,
"amount":8000,
"spread":10,
"tenure":24,
"margin":100,
"loanVariable":{
"type":"roi/commission",
"value":15
}
},
{
"limitRequirementId":"22",
"facilityType":"LC",
"repoRate":4,
"amount":900,
"spread":6,
"tenure":21,
"margin":15,
"loanVariable":{
"type":"roi/commission",
"value":10
}
}
]
},
{
"limitRequirementUniqueId":"13",
"limitRequirementId":"13",
"facilityType":"lc",
"amount":900000,
"existingRoi":10,
"existingPf":1000,
"repoRate":3,
"spread":1,
"tenure":21,
"margin":1000,
"loanVariable":{
"type":"roi/commission",
"value":15
},
"limitBreakup":[
{
"limitRequirementId":"35",
"facilityType":"CC",
"repoRate":6,
"amount":600,
"spread":8,
"tenure":28,
"margin":13,
"loanVariable":{
"type":"roi/commission",
"value":14
}
}
]
}
]
My Solution using normal iteration works:
data.forEach((d, i)=>{
let limitBreakup = d.limitBreakup;
if(Array.isArray(limitBreakup)){
limitBreakup.forEach((l)=>{ console.log(l, '->', l.amount) })
}else{
console.log(limitBreakup, 'else->', limitBreakup.amount)
}
//console.log(d.limitBreakup);
})
But using Recursion, I am half way:
https://jsfiddle.net/1g98sLw3/2/
http://www.mocky.io/v2/5ea974eb3400005e003f0203 (Since the json object is very big, I have pasted in mocky.io for reference)
Something like this should work
Demo proof: https://jsfiddle.net/do58kj3q/
You need a loop to pass through your objects and then add them to the array when you meet the criteria
var data = [{
"limitRequirementId": "123",
"facilityType": "cc",
"amount": 800000,
"existingRoi": 12,
"existingPf": 12100,
"repoRate": 5,
"spread": 10,
"tenure": 24,
"margin": 10000,
"loanVariable": {
"type": "roi/commission",
"value": 15
},
"limitBreakup": [{
"limitRequirementId": "13",
"facilityType": "cc",
"repoRate": 5,
"amount": 8000,
"spread": 10,
"tenure": 24,
"margin": 100,
"loanVariable": {
"type": "roi/commission",
"value": 15
}
},
{
"limitRequirementId": "22",
"facilityType": "LC",
"repoRate": 4,
"amount": 900,
"spread": 6,
"tenure": 21,
"margin": 15,
"loanVariable": {
"type": "roi/commission",
"value": 10
}
}
]
},
{
"limitRequirementUniqueId": "13",
"limitRequirementId": "13",
"facilityType": "lc",
"amount": 900000,
"existingRoi": 10,
"existingPf": 1000,
"repoRate": 3,
"spread": 1,
"tenure": 21,
"margin": 1000,
"loanVariable": {
"type": "roi/commission",
"value": 15
},
"limitBreakup": [{
"limitRequirementId": "35",
"facilityType": "CC",
"repoRate": 6,
"amount": 600,
"spread": 8,
"tenure": 28,
"margin": 13,
"loanVariable": {
"type": "roi/commission",
"value": 14
}
}]
}
]
var array=[];
function recursiveCounter(arr) {
for (let i = 0; i < arr.length; i++) {
const obj = arr[i];
if (!obj.limitBreakup) {
array.push(obj.amount);
}
if (Array.isArray(obj.limitBreakup)) {
recursiveCounter((obj.limitBreakup));
}
}
}
recursiveCounter(data);
console.log(array)

Filter objects by minimum value attributes in javascript

I have an array of objects like below:
[
{
"id": 100,
"Name": "T1",
"amt": 15,
},
{
"id": 102,
"Name": "T3",
"amt": 15,
},
{
"id": 100,
"Name": "T1",
"amt": 20,
},
{
"id": 105,
"Name": "T6",
"amt": 15,
}
]
I want to filter the objects in the array by the minimum of amt. There are two objects with id's 100 but different amt (15 and 20). I want to filter the minimum value which is 15. The output should be:
[
{
"id": 100,
"Name": "T1",
"amt": 15,
},
{
"id": 102,
"Name": "T3",
"amt": 15,
},
{
"id": 105,
"Name": "T6",
"amt": 15,
}
]
I followed this post but does not fit with my problem.
Is there any simpler way of doing this, either pure JavaScript or lodash?
You could group by id and take from every group the object with min value of amt.
var data = [{ id: 100, Name: "T1", amt: 15 }, { id: 102, Name: "T3", amt: 15 }, { id: 100, Name: "T1", amt: 20 }, { id: 105, Name: "T6", amt: 15 }],
result = _(data)
.groupBy('id')
.map(group => _.minBy(group, 'amt'))
.value();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Use the standard algorithm for finding min value and apply the approach to the reduce function. When you find the min or the equal value to the min, add the current object to the array.
const arr = [{
"id": 100,
"Name": "T1",
"amt": 15,
},
{
"id": 102,
"Name": "T3",
"amt": 15,
},
{
"id": 100,
"Name": "T1",
"amt": 20,
},
{
"id": 105,
"Name": "T6",
"amt": 15,
}
]
const minArr = arr.reduce((acc, curr) => curr.amt <= acc.min ? {
...acc,
min: curr.amt,
arr: [...acc.arr, curr]
} : acc, {
min: Infinity,
arr: []
}).arr
console.log(minArr);
You can do this using a for loop like so:
var minimum = 5;
for(var i = 0; i < yourArray; i++) {
if(yourArray[i].amt < minimum) {
console.log("The " + i + " item in the array's amount is less than the minimum: " + minimum);
}
}
Or you can use Array.filter:
var minimum = 5;
function isBigEnough(value) {
return value >= minimum
}
someArray.filter(isBigEnough)

Array transformation in D3

What is best way to transform array from this
[{
"state": "vic",
"age_group_1": 10,
"age_group_2": 20,
"age_group_3": 30,
"age_group_4": 40,
"age_group_5": 50,
}, {
"state": "nsw",
"age_group_1": 60,
"age_group_2": 70,
"age_group_3": 80,
"age_group_4": 90,
"age_group_5": 100,
}, {
"state": "tas",
"age_group_1": 11,
"age_group_2": 21,
"age_group_3": 31,
"age_group_4": 41,
"age_group_5": 51,
}, {
"state": "qld",
"age_group_1": 61,
"age_group_2": 71,
"age_group_3": 81,
"age_group_4": 91,
"age_group_5": 101,
}]
to this
[{
"age_group": "age_group_1",
"states": [{
"name": "vic",
"value": 10
}, {
"name": "nsw",
"value": 60
}, {
"name": "tas",
"value": 11
}, {
"name": "qld",
"value": 61
}]
}, {
"age_group": "age_group_2",
"states": [{
"name": "vic",
"value": 20
}, {
"name": "nsw",
"value": 70
}, {
"name": "tas",
"value": 21
}, {
"name": "qld",
"value": 71
}]
}, {
"age_group": "age_group_3",
"states": [{
"name": "vic",
"value": 30
}, {
"name": "nsw",
"value": 80
}, {
"name": "tas",
"value": 31
}, {
"name": "qld",
"value": 81
}]
}, {
"age_group": "age_group_5",
"states": [{
"name": "vic",
"value": 40
}, {
"name": "nsw",
"value": 90
}, {
"name": "tas",
"value": 41
}, {
"name": "qld",
"value": 91
}]
}, {
"age_group": "age_group_5",
"states": [{
"name": "vic",
"value": 50
}, {
"name": "nsw",
"value": 100
}, {
"name": "tas",
"value": 51
}, {
"name": "qld",
"value": 101
}]
}]
using simple javascript and looping I can do but I want to use either d3 functions or may be any other library that deal with data transformation.
Which is best library for data transformation.
You could use a temporary object for the reference to the result array.
var data = [{ "state": "vic", "age_group_1": 10, "age_group_2": 20, "age_group_3": 30, "age_group_4": 40, "age_group_5": 50, }, { "state": "nsw", "age_group_1": 60, "age_group_2": 70, "age_group_3": 80, "age_group_4": 90, "age_group_5": 100, }, { "state": "tas", "age_group_1": 11, "age_group_2": 21, "age_group_3": 31, "age_group_4": 41, "age_group_5": 51, }, { "state": "qld", "age_group_1": 61, "age_group_2": 71, "age_group_3": 81, "age_group_4": 91, "age_group_5": 101, }],
result = [];
data.forEach(function (a) {
Object.keys(a).forEach(function (k) {
if (k !== 'state') {
if (!this[k]) {
this[k] = { age_group: k, states: [] };
result.push(this[k]);
}
this[k].states.push({ name: a.state, value: a[k] });
}
}, this);
}, {});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Take advantage of modern JS instead of trying to make D3 do it - it's pretty simple code:
var basedata = the original array from your question;
// I assume your data already has this array of categories somewhere,
// but if it doesn't, let's just declare it here:
var age_groups = [
"age_group_1",
"age_group_2",
"age_group_3",
"age_group_4",
"age_group_5"
];
var transformed = age_groups.map(group => {
return {
age_group: group,
states: basedata.map(set => {
return { state: set.state, value: set[group] }
})
};
});
And done.
The reason this works is because you want objects keyed on your age groups, so we start by making sure we create a mapping based on those. Then, for each age group, we simply run through the base data and return the state/value pair. Nothing really complex, and ES6/ES2015 does the job for us.
Don't like ES6/ES2015? Good opportunity to get with the times, but also really easily rewritten to older ES5 format: replace each (...) => { ... } with function(...) { return ... } and done.
You can try something like this:
var data=[{state:"vic",age_group_1:10,age_group_2:20,age_group_3:30,age_group_4:40,age_group_5:50},{state:"nsw",age_group_1:60,age_group_2:70,age_group_3:80,age_group_4:90,age_group_5:100},{state:"tas",age_group_1:11,age_group_2:21,age_group_3:31,age_group_4:41,age_group_5:51},{state:"qld",age_group_1:61,age_group_2:71,age_group_3:81,age_group_4:91,age_group_5:101}];
var _tmp = {}
var kepReg = /^age_group/;
data.forEach(function(item) {
for (var k in item) {
if (kepReg.test(k)) {
if (!_tmp[k])
_tmp[k] = {
state: []
};
_tmp[k].state.push({
name: item["state"],
value: item[k]
})
}
}
});
var result = Object.keys(_tmp).map(function(k) {
return {
age_group: k,
states: _tmp[k].state
}
});
document.write("<pre>" + JSON.stringify(result, 0, 4) + "</pre>")

Parse json to especific format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How i can parse this json
[
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
]
To this format
series: [{
name: '1',
data: [78, 122, 102, 65, 134, 88, 77, 58, 67, 69, 0, 0]
}, {
name: '2',
data: [0, 0, 0, 0, 4, 87, 81, 105, 112, 85, 0, 0]
}]
With an javascript or jquery function efficiently?
In the Json every row represents a total value for each code (1 or 2) per month
For the CODE=1 i have totals only for ten months (month 1 = jan, month 2= feb ... month 10= oct)
For the CODE=2 i have totals only for five months between may and octuber.
In this array [78, 122, 102, 65, 134, 88, 77, 58, 67, 69, 0, 0]
78 is the total for january, 122 for february, 102 for march ...
For those months that i do not have a total, like november or december for example , i need to put a 0
My json will return more info, with more codes i can't use brute force
Thanks in advance
JC
You can run a loop like the following:
var temp = [];
for (var i =0; i< data.length;i++) {
var obj = data[i],
dup = false;
for (var j = 0; j < temp.length; j++) {
var item = temp[j];
if (obj.CODE == item.name) {
item.data.push(obj.TOTAL);
dup = true;
break;
}
}
if (!dup) {
temp.push({
name: obj.CODE,
data: [obj.TOTAL]
});
}
}
var data = [{
"CODE": "1",
"MONTH": "1",
"TOTAL": 78
}, {
"CODE": "1",
"MONTH": "2",
"TOTAL": 122
}, {
"CODE": "1",
"MONTH": "3",
"TOTAL": 102
}, {
"CODE": "1",
"MONTH": "4",
"TOTAL": 65
}, {
"CODE": "1",
"MONTH": "5",
"TOTAL": 134
}, {
"CODE": "1",
"MONTH": "6",
"TOTAL": 88
}, {
"CODE": "1",
"MONTH": "7",
"TOTAL": 77
}, {
"CODE": "1",
"MONTH": "8",
"TOTAL": 58
}, {
"CODE": "1",
"MONTH": "9",
"TOTAL": 67
}, {
"CODE": "1",
"MONTH": "10",
"TOTAL": 69
},
{
"CODE": "2",
"MONTH": "5",
"TOTAL": 4
}, {
"CODE": "2",
"MONTH": "6",
"TOTAL": 87
}, {
"CODE": "2",
"MONTH": "7",
"TOTAL": 81
}, {
"CODE": "2",
"MONTH": "8",
"TOTAL": 105
}, {
"CODE": "2",
"MONTH": "9",
"TOTAL": 112
}, {
"CODE": "2",
"MONTH": "10",
"TOTAL": 85
}
];
var temp = [];
for (var i = 0; i < data.length; i++) {
var obj = data[i],
dup = false;
for (var j = 0; j < temp.length; j++) {
var item = temp[j];
if (obj.CODE == item.name) {
item.data.push(obj.TOTAL);
dup = true;
break;
}
}
if (!dup) {
temp.push({
name: obj.CODE,
data: [obj.TOTAL]
});
}
}
console.log(temp);
<p>Check browser console <sup>F12</sup> to see the result<p>
Here is a brute force example which does only what was requested.
working jsfiddle
var originalData = [
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
];
var newData={};
var tempData1=[];
var tempData2=[];
for (var ii=0;ii<originalData.length;ii++){
if (originalData[ii].CODE==="1"){
tempData1.push(originalData[ii].TOTAL);
}
else{
tempData2.push(originalData[ii].TOTAL);
}
}
newData['series'] =[ {name: '1',
data: tempData1},
{
name: '2',
data: tempData2
}];
console.log(JSON.stringify(newData));
var oData = [
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
],
nData = [{ name: '1', data: [] }, { name: '2', data: [] }];
for (var n = 1; n <= 12; n++) {
var d1 = oData.filter(function(d) {
return d.CODE == '1' && d.MONTH == n;
}),
d2 = oData.filter(function(d) {
return d.CODE == '2' && d.MONTH == n;
});
nData[0].data.push(d1.length ? d1[0].TOTAL : 0);
nData[1].data.push(d2.length ? d2[0].TOTAL : 0);
}
$('pre.out').text( JSON.stringify(nData) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre class="out"></pre>

Categories

Resources