how to use lodash to sum data with the same key? - lodash

I have data like this:
var data = [
{2: 1, 6: 1},
{2: 2},
{1: 3, 6: 2},
];
(the "2" is like a key and "1" means "count")
and I want to output like this:
output = [
{2: 3, 6: 3, 1: 3},
];
is there a way to archive this by using lodash?

Use _.mergeWith() with spread to merge all keys and sum their values:
const data = [{2: 1, 6: 1}, {2: 2}, {1: 3, 6: 2}];
const result = _.mergeWith({}, ...data, (objValue, srcValue) =>
_.isNumber(objValue) ? objValue + srcValue : srcValue);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

Related

Filtering numbers or strings in a comma delimited object

I am using multi-select to filter out data.
<Multiselect
v-model="roles"
class="input1"
placeholder="Select Roles"
mode="tags"
:searchable="true"
:options="roleOptions"
/>
<Multiselect
v-model="sub_organization"
class="input1"
placeholder="Select sub-organization"
mode="tags"
:searchable="true"
:options="suborgOptions"
/>
data:() => ({
mode: "tags",
closeOnSelect: false,
roleOptions: [],
suborgOptions: [],
searchable: true,
sub_organization: [],
roles: [],
filteredData: [],
fetchedData: [],
}),
searchResult() {
this.filteredData = this.fetchedData.filter((data) => {
// var intRoles = parseInt(data.roles.split(", "))
// var intSuborgs = parseInt(data.suborgs.split(", "))
return (
// intSuborgs == this.sub_organization &&
// intRoles == this.roles
data.suborgs.includes(this.sub_organization) &&
data.roles.includes(this.roles)
);
});
},
data.roles = {1, 3},
{1, 4, 5, 7},
{10, 14},
{1, 9},
{2, 4, 6, 8},
{4, 5},
{4, 10},
{9, 1, 4}
for example:
when I use includes and I searched 1 it returns all data.roles with 1 in it including 10, 14, 4, 10 etc.
using includes():
searching 1 returns {1, 3}, {1, 4, 5, 7}, {10, 14}, {1, 9}, {4, 10}, {9, 1, 4}
searching 4 returns {1, 4, 5, 7}, {10, 14}, {4, 10}, {9, 1, 4}, {4, 5}, {2, 4, 6, 8}
as you can see I commented out intRoles and intSuborgs, I tried using parseInt and then split it, when I search 1 it returns only the objects that have 1 in index 0
using parseInt and split:
searching 1 returns {1, 3}, {1, 4, 5, 7}, {1, 9}
searching 4 returns {4, 5}, {4, 10},
What I want to happen is when I search 1 it would return only the objects that has 1 in it excluding double digits with 1, or since I am using multi-select searching 1 and 4 returns objects with 1 and 4 in it excluding double digits with 1 and 4 also.

Filtering an array from another array in vue

This might be something really simple, but I just can't figure it out. What I'm trying to do is take 2 arrays and filter out what I don't need and only
return the one array.
So what I have right now is this
let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];
and what I would like is to return array 2 with only the items that doesn't show up in array1 so that would be 4, 5,6.
This is what I have so far
return array1.forEach(a => {
array2.filter(aa => aa !== a)
});
and that doesn't return anything
let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];
let array3 = array2.filter(i => !array1.includes(i));
console.log(array3)
This might help to solve your problem.
let array1 = [1, 2, 3]
let array2 = [1, 2, 3, 4, 5, 6]
function returnList(arOne,arTwo){
return arTwo.filter(a => !arOne.includes(a))
}
let response = returnList(array1 ,array2 );

How input data in line chart in Pentaho?

I want to make this chart in Pentaho CDE:
based in this chart (I think that is the most similar from among CCC Components):
(The code is in this link.)
but I don't know how I can adapt my data input to that graph.
For example, I want to consume the data with this format:
[Year, customers_A, customers_B, cars_A, cars_B] [2014, 8, 4, 23, 20]
[2015, 20, 6, 30, 38]
How I can input my data in this chart?
Your data should come as an object such as this:
data = {
metadata: [
{ colName: "Year", colType:"Numeric", colIndex: 1},
{ colName: "customers_A", colType:"Numeric", colIndex: 2},
{ colName: "customers_B", colType:"Numeric", colIndex: 3},
{ colName: "cars_A", colType:"Numeric", colIndex: 4},
{ colName: "cars_B", colType:"Numeric", colIndex: 5}
],
resultset: [
[2014, 8, 4, 23, 20],
[2015, 20, 6, 30, 38]
],
queryInfo: {totalRows: 2}
}

Check if the value of key in an array of objects is same - Lodash

Want to check if either the values of all rows in the array of objects is same or the values of all columns in the array of objects is same. How do I efficiently do this with lodash?
[
{row: 0, col: 4},
{row: 0, col: 1},
{row: 0, col: 2}
]
In the above case all rows in the array of objects is same.
If I understood the constraints correctly, how about this?
var data = [
{row: 0, col: 4},
{row: 0, col: 1},
{row: 0, col: 2},
];
function allTheSame(data, key) {
return data.length && _.every(data, function (item) {
return item[key] === data[0][key];
});
}
console.log(_.any(['row', 'col'], function (key) {
return allTheSame(data, key);
}));
Note that _.every and _.any both bail out early, so this should be about as efficient as possible.
Also note that allTheSame returns false if there are no elements in the array. You could switch to return data.length === 0 || _.every(... instead if you want an empty list to be considered "all the same."
The tricky part is trying to check if both column or row values are the same, in the same iteration with lodash. However, that shouldn't be necessary unless your are dealing with an unspeakable amount of data. Assuming that is not the case, here's simple, linear approach:
function isRowOrColSame(data) {
var row = _.every(data, {'row': data[0].row})
var col = _.every(data, {'col': data[0].col})
return (row || col)
}
var data = [
{row: 0, col: 4},
{row: 0, col: 1},
{row: 0, col: 2}
]
console.log(isRowOrColSame(data))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
If you need an explanation as to what's going on with the every function, let me know. I hope that helps!

objective-c equivalent to group by in groovy

Source array:
[ { a: 1, b: 1}, { a: 1, b: 2}, { a: 2, b: 3} ]
Target dictionary:
{ 1: [{a: 1, b: 1}, {a: 1, b: 2}], 2: [{ a: 2, b: 3}] }
So i want to have the objects in the source array grouped by their value of a.
In groovy it's done using array.groupBy({ it.a }). Is there a nice equivalent in objective-c?