I have been struggling with this problem the whole day.
I have got the following array:
controller.array = [{
Equity: "0",
Bond: "97.42",
Cash: "67.98"
}, {
Equity: "5.32",
Bond: "13.12",
Cash: "8"
}, {
// ...
} /* ... */ ]
What I want to do is create a single array containing objects with combined values like so:
controller.newArray = [{
Type: "Equity",
Percentage: "5.32"
}, {
Type: "Bond",
Percentage: "110.54"
}, {
Type: "Cash",
Percentage: "75.98"
} /* ... */ ]
I have tried using _.each like this:
.map(function(item, value) {
var array = [];
_.each(item, function(value, item) {
array.push({
'Source': item,
'Percentage': value
})
})
return array;
})
.value()
What then happens is that it returns an array, containing multiple arrays with objects with my values. Now my problem is that I cant seem to combine all the arrays that are being returned.
Any ideas? Please?
You can transpose the array of objects into an array of values grouped by their common key.
Then you can map the values over to the resulting objects.
The transpose() and sum() functions are underscore mixins, so you can chain them!
_.mixin({
transpose : function(array) {
return _.chain(array).map(_.keys).flatten().uniq().reduce(function(result, key) {
result[key] = _.pluck(array, key);
return result;
}, {}).value();
},
sum : function(values) {
return _.reduce(values, function(sum, value) {
return sum + (_.isNumber(value) ? value : parseFloat(value));
}, 0);
}
});
var array = [{
Equity: "0",
Bond: "97.42",
Cash: "67.98"
}, {
Equity: "5.32",
Bond: "13.12",
Cash: "8"
}];
var result = _.chain(array).transpose().map(function(value, key) {
return {
Type: key,
Percentage: _.sum(value).toFixed(2)
};
}).value();
console.log(JSON.stringify(result, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
One way, using only JavaScript could be to use map and reduce functions:
var data = [{Equity: "0", Bond: "97.42", Cash: "67.98"},
{Equity: "5.32", Bond: "13.12", Cash: "8"}];
var sumMap = data.reduce(function(acc, item) {
Object.keys(item).forEach(function(itemKey) {
if (acc[itemKey] === undefined) {
acc[itemKey] = 0;
}
acc[itemKey] += parseFloat(item[itemKey]);
});
return acc;
}, {});
var result = Object.keys(sumMap).map(function(itemKey) {
return {
"Type": itemKey,
"Percentage": "" + sumMap[itemKey]
};
});
console.log(JSON.stringify(result, null, 2));
The intermediate result sumMap will be something like this:
{
Equity: 5.32,
Bond: 110.54,
Cash: 75.98
}
The fiddle (thanks to CPHPython).
If what you need is to sum each type then this should do:
keys = {Equity: 0, Bond: 0, Cash: 0}
//newArray is the array from your question
newArray.forEach(function(obj) {
keys[obj.Type] += obj.Percentage})
Related
I would like to know how to convert object properties string to integer in javascript.
I have a obj, which if has property value is number string convert to number in javascript
var obj={
ob1: {id: "21", width:"100",height:"100", name: "image1"},
ob2: {id: "22", width:"300",height:"200", name: "image2"}
}
function convertIntObj (obj){
Object.keys(obj).map(function(k) {
if(parseInt(obj[k])===NaN){
return obj[k]
}
else{
return parseInt(obj[k]);
}
});
}
var result = convertIntObj(obj);
console.log(result)
Expected Output:
[
{id: 21, width:100,height:100, name: "image1"},
{id: 22, width:300,height:200, name: "image2"}
]
This should do the work:
var obj = {
ob1: {
id: "21",
width: "100",
height: "100",
name: "image1"
},
ob2: {
id: "22",
width: "300",
height: "200",
name: "image2"
}
}
function convertIntObj(obj) {
const res = {}
for (const key in obj) {
res[key] = {};
for (const prop in obj[key]) {
const parsed = parseInt(obj[key][prop], 10);
res[key][prop] = isNaN(parsed) ? obj[key][prop] : parsed;
}
}
return res;
}
var result = convertIntObj(obj);
console.log('Object result', result)
var arrayResult = Object.values(result);
console.log('Array result', arrayResult)
Click "Run code snippet" so see the result
Iterating over Object.keys() twice. If the value corresponding to the key is a number then parseInt the value else set the default value which was present earlier
var obj = {
ob1: { id: "21", width: "100", height: "100", name: "image1" },
ob2: { id: "22", width: "300", height: "200", name: "image2" }
};
var res = {};
Object.keys(obj).forEach(key => {
res[key] = {};
Object.keys(obj[key]).forEach(temp => {
res[key][temp] = !isNaN(obj[key][temp])
? parseInt(obj[key][temp], 10)
: obj[key][temp];
});
return res;
});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Object.entries() and .reduce() methods to iterate over the key value pairs in your data and use Number and Number.isNaN() methods to transform the values appropriately.
const data = {
ob1: {id: "21", width:"100",height:"100", name: "image1"},
ob2: {id: "22", width:"300",height:"200", name: "image2"}
};
const result = Object.entries(data).reduce((r, [k, o]) => {
r[k] = Object.entries(o).reduce((r, [k, v]) => {
let _v = Number(v);
if(!Number.isNaN(_v)) { v = _v; }
return (r[k] = v, r);
}, {});
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hi I would recommend you to use the JSON.stringify() method. It is used to convert object to string which is needed to send data over the web server. It converts the set of variables in the object to a JSON string:
var objToStr = {
siteName: "W3Docs",
bookName: "Javascript",
booksCount: 5
};
var myJSON = JSON.stringify(objToStr);
console.log(myJSON);
Also, you can use the The toString() method. It is also called when you need to convert the object into a string:
var obj = {
siteName: "W3Docs",
bookName: "Javascript",
booksCount: 5
};
function objToString(object) {
var str = '';
for (var k in object) {
if (object.hasOwnProperty(k)) {
str += k + '::' + object[k] + '\n';
This information is taken from this source.
So I have an array of object which looks like this:
var myArray = [{priority : "low"}, {priority: "critical"}, {priority: "high"}]
I need to sort in this way: 1)Critical, 2) High and 3) Low.
how can this be done?
I suggest to use an object for the storing of the sort order.
If you need a default value for sorting, you could use a value for sorting unknown priority to start or to the end.
var sort = ['critical', 'high', 'low'],
defaultValue = Infinity,
sortObj = {},
myArray = [{ priority: "unknown" }, { priority: "low" }, { priority: "critical" }, { priority: "high" }];
sort.forEach(function (a, i) { sortObj[a] = i + 1; });
myArray.sort(function (a, b) {
return (sortObj[a.priority] || defaultValue) - (sortObj[b.priority] || defaultValue);
});
console.log(myArray);
Use an object that maps priority names to numbers, then sort based on that.
var priorities = {
low: 0,
high: 1,
critical: 2
};
var myArray = [{
priority: "low"
}, {
priority: "critical"
}, {
priority: "high"
}]
myArray.sort(function(a, b) {
return priorities[b.priority] - priorities[a.priority];
});
console.log(myArray);
I have a series of JSON entries:
[{"num": "1","name_A": "Alex" ,"name_B": "Bob"}, {"num": "2","name_A": "Anne" ,"name_B": "Barbra"}]
I am trying to convert this array of Objects as painlessly as possible into two objects - one with title name_A, and the second with the title name_B. Objects have to contain the title and an array of matching num-name pairs:
[{title: "name_A", names:[{"1", "Alex}, {"2", "Anne"}]}, {title:"name_B", names: [{"1", "Bob"}, {"2", "Barbra"}]}]
At first I tried simply to create two objects by reducing the array of object twice, once for name_A and second time for name_B and later glue everything together:
// get 'names' array
var name_A = objArray.reduce(function(memo, curr) {
memo.push({curr.num, curr.name_A})
return memo;
}, []);
But even this is failing. Why there is no push method for memo if I initialize reduce with an empty array?
And second question, am I on a right track or is there a better way to achieve this?
Comments inline, made a few minor corrections to the expectations.
var input = [{ "num": "1", "name_A": "Alex", "name_B": "Bob" }, { "num": "2", "name_A": "Anne", "name_B": "Barbra" }]
var output = input.reduce(function (a, b) {
// construct new objects and set their properties
var i = {};
i[b.num] = b.name_A;
var j = {};
j[b.num] = b.name_B;
// add them to our collection elements
a[0].names.push(i);
a[1].names.push(j);
return a;
// initializing our collection
}, [{ title: "name_A", names: [] }, { title: "name_B", names: [] }]);
// pretty print our output
console.log(JSON.stringify(output, null, " "))
var input = [{ "num": "1", "name_A": "Alex", "name_B": "Bob" }, { "num": "2", "name_A": "Anne", "name_B": "Barbra" }]
var output = input.reduce(function (a, b) {
// construct new objects and set their properties
var i = {};
i[b.num] = b.name_A;
var j = {};
j[b.num] = b.name_B;
// add them to our collection elements
a[0].names.push(i);
a[1].names.push(j);
return a;
// initializing our collection
}, [{ title: "name_A", names: [] }, { title: "name_B", names: [] }]);
so.log(output)
<pre id="output"></pre>
<script>
var so = {
log: function(o) {
document.getElementById("output").innerHTML = JSON.stringify(o, null, " ")
}
}
</script>
The problem with your code is that { curr.num, curr.name_A } is not a valid object, it's missing the property names. I've added properties num and name in my code below.
var name_A = [];
var name_B = [];
objArray.forEach(function(curr) {
name_A.push({num: curr.num, name: curr.name_a});
name_B.push({num: curr.num, name: curr.name_B});
});
var result = [
{ title: "name_A" }, names: name_A },
( title: "name_B" }, names: name_B }
];
Also, if you want to make an array out of the results of looping over an array, you should use .map rather than .reduce.
Assuming only property num is fixed. All other properties are treated as data, like name_A or name_B.
var a = [{ "num": "1", "name_A": "Alex", "name_B": "Bob" }, { "num": "2", "name_A": "Anne", "name_B": "Barbra" }],
result = [];
a.forEach(function (el) {
var num = el.num;
Object.keys(el).forEach(function (k) {
function tryFindIndexAndSetNames(aa, i) {
if (aa.title === k) {
result[i].names[num] = el[k];
return true;
}
}
if (k !== 'num' && !result.some(tryFindIndexAndSetNames)) {
var o = {};
o[num] = el[k];
result.push({ title: k, names: o });
}
});
});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
I have JSON as
var newJSON = [{
"key": "India",
"value": "72"
}, {
"key": "India",
"value": "27"
}, {
"key": "Pakistan",
"value": "90"
}, {
"key": "Zimbamwe",
"value": "88"
}, {
"key": "India",
"value": "100"
}, {
"key": "Pakistan",
"value": "172"
}]
I want desired result as below, where the duplicate key values have their value properties added together:
[{
"key": "India",
"value": "199"
}, {
"key": "Pakistan",
"value": "262"
}, {
"key": "Zimbamwe",
"value": "88"
}]
Please help me with this
Here is the solution:
var grouped = [];
var added = [];
for(var i = 0; i < json.length; i++) {
var indexOfCountry = added.indexOf(json[i].key);
if (indexOfCountry >= 0)
{
grouped[indexOfCountry].value = (Number(grouped[indexOfCountry].value) + Number(json[i].value)).toString();
}
else {
grouped.push(json[i]);
added.push(json[i].key);
}
}
grouped array is your desired result.
Fiddle: https://jsfiddle.net/zummw9zp/
Yet another variant with reduce
var result = newJSON.reduce(function(acc, el){
var val = acc.map[el.key];
if(!val){
acc.map[el.key] = val = { key:el.key, value: parseInt(el.value) };
acc.result.push(val);
}else{
val.value += parseInt(el.value);
}
return acc;
},{map:{}, result:[]}).result;
var newJSON = [
{"key":"India","value":"72"},{"key":"India","value":"27"},
{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},
{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}
];
document.getElementById('r').innerHTML = 'newJSON: ' + JSON.stringify(newJSON);
var result = newJSON.reduce(function(acc, el){
var val = acc.map[el.key];
if(!val){
acc.map[el.key] = val = { key:el.key, value: parseInt(el.value) };
acc.result.push(val);
}else{
val.value += parseInt(el.value);
}
return acc;
},{map:{}, result:[]}).result;
document.getElementById('r').innerHTML += '<br /><br />result: ' + JSON.stringify(result);
<div id="r"></div>
This is a classic use case for reduce, which is designed to take arrays and somehow, well, "reduce" them to other things, by looping across them and transforming the result at each iteration.
return newJSON.reduce(function(result, entry) {
key = entry.key;
result[key] = result[key] || { key: key, value: 0 };
result[key].value += entry.value;
return result;
}, {});
Using Underscore
If you're OK with using a library like Underscore, you can write this as
_.mapObject(_.groupBy(newJSON, 'key'), total)
Using a narrative style where we describe
`A(B, C)`
as
Take B and do A to it usingC
and
`A(B(C))`
as
Take C and do B to it. Then take the result and do A to it
we can almost read this as English:
Take newJSON and group it by using key. Then take the result and map the object using total
_.groupBy produces an object keyed by some property and returns groups, which are arrays of all the entries falling into each group:
{
India: [ {key: "India", value: 72}, {key: "India", value: 100... ],
...
}
total calculates the total value for each group:
function total(group) { return sum(group . map(value)); }
So it converts an array of entries
[ {key: "India", value: 72}, {key: "India", value: 100}, ... ],
into 199. We use this to map the arrays of entries to total scores using _.mapObject.
Here sum can be written as
function sum(array) { return array.reduce(add); }
function add(a, b) { return a+b; }
and value is a little utility function to retrieve the value property:
function value(entry) { return entry.value; }
So the complete solution is:
function add (a, b) { return a+b; }
function sum (array) { return array.reduce(add); }
function value (entry) { return entry.value; }
function total (group) { return sum(group . map(value)); }
_.mapObject(_.groupBy(newJSON, 'key'), total)
I have a document that looks like something like this:
{
name: "Johnny Boy",
age: 24,
hobbies: ["fencing", "chess", "raves"],
_createdAt: 2015-05-15T18:12:26.731Z,
_createdBy: "JohnnyBoy",
_id: mQW4G5yEfZtsB6pcN
}
My goal is to return everything that doesn't start with an underscore, and format it a bit differently so I will end up with this:
[
{
fieldName: "name",
value: "Johnny Boy"
},
{
fieldName: "age",
value: 24
},
{
fieldName: "hobbies",
value: ["fencing", "chess", "raves"]
}
]
My initial solution was to run it through the _.map function of the Underscore library (which has nothing to do with my wanting to remove underscores specifically...) and then using lastIndexOf to figure out which keys begin with an underscore:
var listWithoutUnderscores = _.map(myList, function(val, key) {
if (key.lastIndexOf("_", 0) === -1)
return {fieldName: key, value: val}
return null
})
However, this will literally return null instead of the fields that began with _ in the returned array:
[
...
{
fieldname: "hobbies",
value: ["fencing", "chess", "raves"]
},
null,
null,
null
]
I want to remove them completely, ideally within the map function itself, or else by chaining it through some kind of filter but I don't know which one is fastest in this case.
You can use reduce for this:
var listWithoutUnderscores = _.reduce(myList, function(list, val, key) {
if (key.lastIndexOf("_", 0) === -1){
list.push( {fieldName: key, value: val});
}
return list;
}, []);
Underscore also comes with an array method compact which will remove all falsey and null values from an array:
_.compact([0, 1, false, 2, '', null, 3]);
=> [1, 2, 3]
You could just call _.compact(array) on the array after your map.
You can use pick and pass a predicate to get the valid keys and then map across those fields:
var validKey = function(value, key){
return _.first(key) != '_';
}
var createField = function(value, key){
return {
fieldname: key,
value: value
}
}
var result = _.chain(data)
.pick(validKey)
.map(createField)
.value();
var data = {
name: "Johnny Boy",
age: 24,
hobbies: ["fencing", "chess", "raves"],
_createdAt: '2015-05-15T18:12:26.731Z',
_createdBy: "JohnnyBoy",
_id: 'mQW4G5yEfZtsB6pcN'
}
var validKey = function(value, key){
return _.first(key) != '_';
}
var createField = function(value, key){
return {
fieldname: key,
value: value
}
}
var result = _.chain(data)
.pick(validKey)
.map(createField)
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>