How to get the array of objects based on condition in javascript.
I have array object obj in which each objects w1,w2...wn should have count greater than 2.
How to filter the array object based on object key in javascript.
function getObject (obj1){
var result = obj1.filter(e=> e.w1.count > 2 && e.w2.count > 2);
return result;
}
var output = this.getObject(obj1);
var obj1=[
{
"memberid": "s1",
"w1":{"count": 1, "qty": 1},
"w2":{"count": 0, "qty": 0},
... wn
"totalcount": 1
},
{
"memberid": "s2",
"w1":{"count": 2, "qty": 2, "amount": 400.0},
"w2":{"count": 1, "qty": 2, "amount": 503.0},
... wn
"totalcount": 5
},
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
... wn
"totalcount": 6
}
]
Expected Output:
[
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
... wn
"totalcount": 6
}
]
You can filter your array based on every value in each object either not being an object, or if it is an object, having a count greater than 2:
const obj1 = [{
"memberid": "s1",
"w1": {
"count": 1,
"qty": 1
},
"w2": {
"count": 0,
"qty": 0
},
"totalcount": 1
},
{
"memberid": "s2",
"w1": {
"count": 2,
"qty": 2,
"amount": 400.0
},
"w2": {
"count": 1,
"qty": 2,
"amount": 503.0
},
"totalcount": 5
},
{
"memberid": "s3",
"w1": {
"count": 3,
"qty": 2,
"amount": 0.0
},
"w2": {
"count": 3,
"qty": 4,
"amount": 503.0
},
"totalcount": 6
}
];
const out = obj1.filter(o => Object.values(o).every(v => typeof v != 'object' || v.count > 2));
console.log(out);
you need to iterate over the object keys, filtering out the invalid ones
function getObject(obj1) {
// filter
return obj1.filter(e =>
// based on the entries [key, value]
Object.entries(e)
// filter out entries where key is not a w followed by a number
.filter(val => val[0].match(/w\d+/))
// if every selected entry as a count > 2
.every(val => val[1].count > 2)
);
}
const obj1=[{memberid:"s1",w1:{count:1,qty:1},w2:{count:0,qty:0},totalcount:1},{memberid:"s2",w1:{count:2,qty:2,amount:400},w2:{count:1,qty:2,amount:503},totalcount:5},{memberid:"s3",w1:{count:3,qty:2,amount:0},w2:{count:3,qty:4,amount:503},totalcount:6}];
const output = this.getObject(obj1);
console.log(output)
docs of usefull functions :
Object.entries,
Array.filter,
Array.every
function getObject (obj1) {
var result = obj1.filter((e) => {
var isValid = false;
var i = 1;
while (e['w' + i]) {
if (e['w' + i].count > 2) {
isValid = true;
} else {
isValid = false;
break;
}
i++;
}
return isValid;
});
return result;
}
Related
{
"order": [
{
"billDate": "1-apr-2016",
"stock": 2,
"Qty": 4,
"Amount": 4500,
"Available": '',
},
{
"billDate": "1-may-2016",
"stock": 3,
"Qty": 2,
"Amount": 4500,
"Available": '',
}
]
}
I have an array which is sorted by billDate . now i need to add my first Object date Stock (i.e 2) to next object Qty and add into Available field .
Expected Result:
{
"order": [
{
"billDate": "1-apr-2016",
"stock": 2,
"Qty": 5,
"Amount": 4500,
"Available": 0,
},
{
"billDate": "1-may-2016",
"stock": 3,
"Qty": 2,
"Amount": 4500,
"Available": 4,
}
]
}
First Object Stock is 2 and next object Qty is 2 so Available is 4.How to add like above approach ?
I have tried like this
var totalTaxes = order.reduce(function (sum, tax) {
return sum + (tax.Qty + tax.stock);
}, 0);
You don't want reduce for this at all. If your intention is to create an array of the same length with different values, you want map
const { order } = {"order":[{"billDate":"1-apr-2016","stock":2,"Qty":4,"Amount":4500,"Available":""},{"billDate":"1-may-2016","stock":3,"Qty":2,"Amount":4500,"Available":""}]}
const result = {
order: [
{...order[0], Available: 0}, // first entry with Available: 0
...order.slice(1).map((ord, i) => ({ // map the rest
...ord,
Available: ord.Qty + order[i].stock // calculate new Available
}))
]
};
console.log(result);
.as-console-wrapper { max-height: 100% !important; }
I have an array of objects. I want to group them by a specific field.
[
{
"name": "JOHN",
"type": 1,
"sum": 5
},
{
"name": "SERA",
"type": 1,
"sum": 43
},
{
"name": "SERA",
"type": 2,
"sum": 129
},
{
"name": "JOHN",
"type": 2,
"sum": 200
}
]
The output I expect for grouping by name attribute is as follows.
{
// Group #1
"JOHN": [
{
"type": 2,
"sum": 200
}
{
"type": 1,
"sum": 5
}
],
// Group #2
"SERA":[
{
"type": 1,
"sum": 43
},
{
"type": 2,
"sum": 129
},
]
}
I used nested loops, but unfortunately the execution speed was slow and it did not give the right results.
As if you mentioned, we can use an object instead of an array for the most outer wrapper. And also swap inside one object to an array, then this is a possible solution.
var data = [{"name": "JOHN","type": 1,"sum": 5},{"name": "SERA","type": 1,"sum": 43},{"name": "SERA","type": 2,"sum": 129},{"name": "JOHN","type": 2,"sum": 200}];
var newData = {};
data.forEach( (item) => {
if (!(item['name'] in newData)) {
newData[item['name']] = [];
}
newData[item['name']].push(
{
'type': item['type'],
'sum' : item['sum']
}
);
});
console.log(newData);
Your proposed output structure is not valid, however using Array.reduce you can create an object in which all the properties are arrays of objects:
const data = [
{
"name": "JOHN",
"type": 1,
"sum": 5
},
{
"name": "SERA",
"type": 1,
"sum": 43
},
{
"name": "SERA",
"type": 2,
"sum": 129
},
{
"name": "JOHN",
"type": 2,
"sum": 200
}
];
const result = data.reduce((c, {name, type, sum}) => {
c[name] = c[name] || [];
c[name].push({type, sum});
return c;
}, {});
console.log(result);
One more way with forEach, destructuring and ?? operator
const merge = (arr) => {
const obj = {};
arr.forEach(({ name, ...rest }) => (obj[name] ??= []).push(rest));
return obj;
};
const data = [
{
name: "JOHN",
type: 1,
sum: 5,
},
{
name: "SERA",
type: 1,
sum: 43,
},
{
name: "SERA",
type: 2,
sum: 129,
},
{
name: "JOHN",
type: 2,
sum: 200,
},
];
console.log(merge(data));
You can use this function which take advantage of Array.prototype.reduce to transform the initial data to another structure of array.
let data = [
{
"name": "JOHN",
"type": 1,
"sum": 5
},
{
"name": "SERA",
"type": 1,
"sum": 43
},
{
"name": "SERA",
"type": 2,
"sum": 129
},
{
"name": "JOHN",
"type": 2,
"sum": 200
}
];
function groupedBy(data, field) {
let fieldValues = [...data].reduce((acc, current) => {
return acc.concat(current[field]);
}, []).filter((value, index, self) => {
return self.indexOf(value) === index;
});
let results = fieldValues.reduce((acc, item) => {
let items = [...data].filter(el => {
return el.name === item;
});
items.forEach(i => delete i.name);
return Object.assign(acc, { [item]: items});
}, {});
return results;
}
console.log(groupedBy(data, "name"));
I have 3 arrays of 3 different types. Each array contains the count of an id (which might be duplicate like arrayOfB).
Each id has a limit value of count property is 10 (the count includes different types. Ex: if unique1 has 10 counts in type A, when process type B for unique1, it will be not processed).
const arrayOfA = [
{
"type": "A", "count": 10, "id": "UID1"
},
{
"type": "A", "count": 20, "id": "UID2"
},
{
"type": "A", "count": 1, "id": "UID4"
},
];
const arrayOfB = [
{
"type": "B", "count": 5, "id": "UID1"
},
{
"type": "B", "count": 5, "id": "UID3"
},
];
const arrayOfC = [
{
"type": "C", "count": 6, "id": "UID1"
},
{
"type": "C", "count": 6, "id": "UID4"
},
{
"type": "C", "count": 3, "id": "UID2"
},
{
"type": "C", "count": 3, "id": "UID3"
},
]
The output will be like:
Map {
'UID1|A' => 10,
'UID2|A' => 10,
'UID4|A' => 1,
'UID3|B' => 5,
'UID4|C' => 6 }
I used a set to hold id, which already has the maximum count and map to hold the output.
const maxed = new Set();
const elements = new Map();
arrayOfA.forEach(element => {
if (element.count > 10) {
maxed.add(`${element.id}`);
elements.set(`${element.id}|${element.type}`, 10);
console.log(elements)
return;
}
if (elements.has(`${element.id}|${element.type}`)) {
const newCount = elements.get(`${element.id}|${element.type}`) + element.count;
newCount > 10 ? elements.set(`${element.id}|${element.type}`, 10) : elements.set(`${element.id}|${element.type}`, newCount);
console.log(elements)
return;
}
elements.set(`${element.id}|${element.type}`, element.count);
});
arrayOfB.forEach(element => {
if (maxed.has(`${element.id}`)) {
console.log(elements)
return;
}
const countOfA = elements.has(`${element.id}|A`) ? elements.get(`${element.id}|A`) : 0;
let newCount = countOfA + element.count;
if (elements.has(`${element.id}|${element.type}`)) {
newCount = newCount + element.get(`${element.id}|${element.type}`);
}
if (newCount > 10) {
maxed.add(`${element.id}`);
if ((10 - countOfA) > 0) elements.set(`${element.id}|${element.type}`, 10 - countOfA);
console.log(elements)
return;
}
elements.set(`${element.id}|${element.type}`, element.count);
})
arrayOfC.forEach(element => {
if (maxed.has(`${element.id}`)) {
console.log(elements)
return;
}
const countOfA = elements.has(`${element.id}|A`) ? elements.get(`${element.id}|A`) : 0
const countOfB = elements.has(`${element.id}|C`) ? elements.get(`${element.id}|C`) : 0
let newCount = countOfA + countOfB + element.count;
if (elements.has(`${element.id}|${element.type}`)) {
newCount = newCount + element.get(`${element.id}|${element.type}`);
}
if (newCount > 10) {
maxed.add(`${element.id}`);
if ((10 - countOfA - countOfB) > 0); elements.set(`${element.id}|${element.type}`, 10 - countOfA - countOfB);
console.log(elements)
return;
}
elements.set(`${element.id}|${element.type}`, element.count);
})
I want to ask about another faster implementation if any. I estimated my big O will be O(n) (n is the total length of 3 arrays). If elements of arrays do not contain the same id.
Edit:
Big thanks to you all, but seems like there's one edge case. The answers couldn't handle
var arrayOfA = [
{
"type": "A", "count": 10, "id": "UID1"
},
{
"type": "A", "count": 20, "id": "UID2"
},
{
"type": "A", "count": 1, "id": "UID4"
},
];
const arrayOfB = [
{
"type": "B", "count": 5, "id": "UID1"
},
{
"type": "B", "count": 5, "id": "UID3"
},
{
"type": "B", "count": 1, "id": "UID3"
},
];
var arrayOfC = [
{
"type": "C", "count": 6, "id": "UID1"
},
{
"type": "C", "count": 6, "id": "UID4"
},
{
"type": "C", "count": 3, "id": "UID2"
},
{
"type": "C", "count": 3, "id": "UID3"
},
]
In arrayOfB, I have the UID3 occurs twice, so your answers doesn't seem to work on that case.
Instead of a Set for a maxed id, you could sum the count for every id and use it for all following arrays.
const
getKey = (...a) => a.join('|'),
rawData = [{ type: "A", count: 10, id: "UID1" }, { type: "A", count: 20, id: "UID2" }, { type: "A", count: 1, id: "UID4" }],
rawData3 = [{ type: "B", count: 5, id: "UID1" }, { type: "B", count: 5, id: "UID3" }],
rawData2 = [{ type: "C", count: 6, id: "UID1" }, { type: "C", count: 6, id: "UID4" }, { type: "C", count: 3, id: "UID2" }, { type: "C", count: 3, id: "UID3" }],
elements = new Map,
sums = new Map;
[rawData, rawData3, rawData2].forEach(a => a.forEach(({ type, count, id }) => {
var sum = sums.get(id) || 0,
key = getKey(id, type);
sums.set(id, sum + count);
if (sum >= 10) return;
if (sum + count > 10) {
if (10 - sum > 0) elements.set(key, 10 - sum);
return;
}
elements.set(key, count);
}));
[...elements].map(a => console.log(a.join(': ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Based on the assumption that you have missed to include "B" in your expected results set, two nested loops can provide the manipulation and filtering you want.
function getIdSummary(arrays) {
const maxValue = 10;
//Array of objects which we later conver to a map
//The aim is ease of indexing during the iterations
var summary = []
//A heler to find if a maxed uid is found in the summary
function isMaxed(uid) {
return summary.some(item => {
return item.uid === uid && item.count >= maxValue;
})
}
//Iterate all the arrays
arrays.forEach(anInputArray => {
//Iterate each array
anInputArray.forEach(item => {
if (!isMaxed(item.id)) {
summary.push({uid: item.id, type: item.type, count: item.count > maxValue ? 10 : item.count})
}
})
})
return new Map(summary.map(obj => {
return [obj.uid + '|' + obj.type, obj.count]
}))
}
var arrayOfA = [
{
"type": "A", "count": 10, "id": "UID1"
},
{
"type": "A", "count": 20, "id": "UID2"
},
{
"type": "A", "count": 1, "id": "UID4"
},
];
const arrayOfB = [
{
"type": "B", "count": 5, "id": "UID1"
},
{
"type": "B", "count": 5, "id": "UID3"
},
];
var arrayOfC = [
{
"type": "C", "count": 6, "id": "UID1"
},
{
"type": "C", "count": 6, "id": "UID4"
},
{
"type": "C", "count": 3, "id": "UID2"
},
{
"type": "C", "count": 3, "id": "UID3"
},
]
var m = getIdSummary([arrayOfA, arrayOfB, arrayOfC]);
console.log(Array.from(m));
I have the below object which has array of objects. I want format this object to object of Array of arrays
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = [];
let total = [];
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.
Expected output is
var object1={
"Usa":[
["123245235",1],
["21423435",2]
],
"india":[
["234325436",2],
["23422464",3]
]
}
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
This is an alternative using the function Object.entries to get the entries as follow [country, array] and then with the function reduce build the desired output.
The function map will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys & reduce:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
I have two JSON arrays with same fields as follows:
var total_90 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 90, "tip": 1, "type": "tab" },
{ "date": "2012-11-14T16:30:43Z", "quantity": 3, "total": 90, "tip": 0, "type": "tab" }
];
var tip_0 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 80, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 70, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:58:03Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:30:43Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" }
];
I need a third JSON file which has the intersection of the above two JSON files. (By intersection, I mean all the rows from both the JSON files which have TOTAL=90 AND TIP=0)
Is there some way to do this?
My expected output will be a third JSON file with the following output
{"date":"2012-11-14T16:30:43Z","quantity":3,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:58:03Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:30:43Z","quantity":2,"total":90,"tip":0,"type":"tab"}
You need to loop the 2 objects and merge the contents into 1 object.
For examples check this thread since this is a duplicate How can I merge properties of two JavaScript objects dynamically?
You could do the following to collect all the rows from both the JSON files which have TOTAL = 90 and TIP = 0 -
var total_90 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 90, "tip": 1, "type": "tab" },
{ "date": "2012-11-14T16:30:43Z", "quantity": 3, "total": 90, "tip": 0, "type": "tab" }
];
var tip_0 = [
{ "date": "2011-11-14T17:22:59Z", "quantity": 2, "total": 80, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T17:07:21Z", "quantity": 2, "total": 70, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:58:03Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" },
{ "date": "2011-11-14T16:30:43Z", "quantity": 2, "total": 90, "tip": 0, "type": "tab" }
];
// An empty arrays to contain final intersection array
var result = [];
/* now looping over both arrays to traverse all the elements from them */
// iterating over first array
total_90.forEach(x => {
// iterating over second array
tip_0.forEach(y => {
// push into output array only if total is 90 & tip is 0
if ((x.total == 90 && y.total == 90) && (x.tip == 0 && y.tip == 0)) {
result.push({
date: x.date,
quantity: x.quantity,
total: x.total,
tip: x.tip,
type: x.type
});
}
});
});
console.log(result);
Note - this can be optimized to reduce the time complexity.
function intersection(a, b)
{
var result = [];
for (var i = 0; i < a.length; i++){
if (a[i].total == 90 && a[i].tip == 0)
{
result.push(a[i]);
}
}
for (var i = 0; i < b.length; i++){
if (b[i].total == 90 && b[i].tip == 0)
{
result.push(b[i]);
}
}
return result;
}
JSFiddle
EDIT: Update the function with the use of concat to provide a slightly more short hand.
function intersection(a, b)
{
var result = [];
var c = a.concat(b);
for (var i = 0; i < c.length; i++){
if (c[i].total == 90 && c[i].tip == 0)
{
result.push(c[i]);
}
}
return result;
}
New JSFiddle
var total_90 = [
{"date":"2011-11-14T17:22:59Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T17:07:21Z","quantity":2,"total":90,"tip":1,"type":"tab"},
{"date":"2012-11-14T16:30:43Z","quantity":3,"total":90,"tip":0,"type":"tab"}
]
var tip_0 = [
{"date":"2011-11-14T17:22:59Z","quantity":2,"total":80,"tip":0,"type":"tab"},
{"date":"2011-11-14T17:07:21Z","quantity":2,"total":70,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:58:03Z","quantity":2,"total":90,"tip":0,"type":"tab"},
{"date":"2011-11-14T16:30:43Z","quantity":2,"total":90,"tip":0,"type":"tab"}
]
allData['total_90'] = total_90;
allData['tip_0'] = tip_0;
use allData