how to group JSON objects from array of JSON objects? - javascript

Here is an array of JSON objects that has array values that I want to group by (pull unique):
let myArray = [
{
"_id": "1",
"subdata": [
{
"subid": "11",
"name": "A"
},
{
"subid": "12",
"name": "B"
}
]
},
{
"_id": "2",
"subdata": [
{
"subid": "12",
"name": "B"
},
{
"subid": "33",
"name": "E"
}
]
}
]
Finally I need to get:
[
{
"subid": "11",
"name": "A"
},
{
"subid": "12",
"name": "B"
},
{
"subid": "33",
"name": "E"
}
]
I've tried lodash with no success:
let newArray = lodash.uniqBy(lodash.concat(myArray, 'subdata.subid'), '_id');
Of course I can scan each array element one by one but thought there is easy way to do it with lodash

Use _.flatMap() to get the an array of all subdata items, and then use _.uniqueBy() with subid:
const myArray = [{"_id":"1","subdata":[{"subid":"11","name":"A"},{"subid":"12","name":"B"}]},{"_id":"2","subdata":[{"subid":"12","name":"B"},{"subid":"33","name":"E"}]}];
const result = _.uniqBy(_.flatMap(myArray, 'subdata'), 'subid');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
With lodash/fp you can generate a function using _.flow() with the same methods:
const fn = _.flow(
_.flatMap('subdata'),
_.uniqBy('subid')
);
const myArray = [{"_id":"1","subdata":[{"subid":"11","name":"A"},{"subid":"12","name":"B"}]},{"_id":"2","subdata":[{"subid":"12","name":"B"},{"subid":"33","name":"E"}]}];
const result = fn(myArray);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash#4(lodash.min.js+lodash.fp.min.js)'></script>

Related

How can i filter result if i have array in body

i have a payload
{
"category": "Mobile",
"price": {
"from": "10",
"to": "50"
},
"location": [
"Jakrta",
"Bandung",
"Surabaya"
],
"rating": [
"1",
"2",
"3"
]
}
i want to find all object which have rating 1 or 2 or 3 and also have any location
Basically i am creating a filter for an ecommerce store i which we will get multiple location and multiple ratings as well so we will return only those object which have matched property. i am attaching a screenshot of UI for better understanding.
i want to run this filter with multiple location and multiple checked checkbox
You can do create a filter dynamically:
const { category, price, location, rating } = req.body;
const filter = {};
if (category) filter.category = category;
if (price) filter.price = { $gte: parseInt(price.from, 10), $lte: parseInt(price.to, 10) };
if (location?.length) filter.location = { $in: location };
if (rating?.length) filter.rating = { $in: rating };
const data = await Collection.find(filter);
If you want to filter your objects, you should use filter() from your array :
const arr = [{
"category": "Mobile1",
"price": {
"from": "10",
"to": "50"
},
"location": [
"Jakrta",
"Bandung",
"Surabaya"
],
"rating": [
"1",
"2",
"3"
]
},
{
"category": "Mobile2",
"price": {
"from": "10",
"to": "50"
},
"location": [
"Jakrta",
"Bandung",
"Surabaya"
],
"rating": [
"2",
"3"
]
}];
const result = arr.filter(el => el.rating.includes("1") || el.rating.includes("2") || el.rating.includes("3"));
console.log(result);

How can I inner join with two object arrays in JavaScript?

I need inner join with two array in javascript like this:
array1 =
[
{
"id": 1,
"name": "Tufan"
},
{
"id": 2,
"name": "Batuhan"
},
{
"id": 3,
"name": "Hasan"
}
]
array2 =
[
{
"name": "yyy",
"externalid": "1",
"value": "Asd"
},
{
"name": "aaaa"
"externalid": "2",
"value": "ttt"
}
]
expectedArray =
[
{
"id": 1,
"name": "Tufan",
"externalid": "1",
"value": "Asd"
},
{
"id": 2,
"name": "Batuhan",
"externalid": "2",
"value": "ttt"
}
]
rules:
on: array2.externalid = array1.id
select: array1.id, array1.name, array2.externalid, array2.value
My approach:
array1.filter(e => array2.some(f => f.externalid == e.id));
// I need help for continue
How can I make this?
Doesn't matter information: I use ES5 and pure javascript
You can do it like this:
const res = array2.map((item) => {
const related = array1.find((el) => el.id == item.externalid);
return { ...item, ...related };
});
Using a map to loop over the array2 and a find to get the array1 relative.

Iterate array of objects with string array [duplicate]

This question already has answers here:
Filter array of objects based on another array in javascript
(10 answers)
Closed 2 years ago.
I have an array of objects (JAVASCRIPT) like below
[
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
]
And i have a string array like below
["123", "234"]
I want to loop through array of objects and pass string array to get the "label"
I am expecting an output like below
[
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
}
]
Please let me know if there is any efficient solution (JAVASCRIPT) because my array of objects is big.
Try this:
const obj = [
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
];
const arr = ["123", "234"];
var output = arr.flatMap(item => obj.filter(x => x.code == item));
console.log(output);
If the array is big, this can help to use Array.reduce.
const input = [{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
]
const input2 = ["123", "234"];
const inputObj =input.reduce((acc, cur) => {
acc[cur.code] = cur.label;
return acc;
}, {});
const result = input2.reduce((acc, cur) => {
if (inputObj[cur]) {
acc.push({
code: cur,
label: inputObj[cur]
});
}
return acc;
}, []);
console.log(result);

Comparing arrays of objects and remove duplicate [duplicate]

This question already has answers here:
Remove duplicates in an object array Javascript
(10 answers)
Closed 5 years ago.
I have an array containing arrays of objects which I need to compare.
I've looked through multiple similar threads, but I couldn't find a proper one that compares multiple arrays of objects (most are comparing two arrays of objects or just comparing the objects within a single array)
This is the data (below is a JSFiddle with code sample)
const data = [
[
{
"id": "65",
"name": "Some object name",
"value": 90
},
{
"id": "89",
"name": "Second Item",
"value": 20
}
],
[
{
"id": "89",
"name": "Second Item",
"value": 20
},
{
"id": "65",
"name": "Some object name",
"value": 90
}
],
[
{
"id": "14",
"name": "Third one",
"value": 10
}
]
]
I want to remove all duplicate arrays of objects, regardless of the length of data (there could be a lot more records).
I managed to get the unique ones extracted into an object:
const unique = data.reduce(function(result, obj) {
return Object.assign(result, obj)
}, [])
That doesn't work for me though, because I need 1 of the duplicated arrays to remain and the returned data to be an array as well, instead of an object. E.g.:
// result I need
[
[
{
"id":"65",
"name":"Some object name",
"value":90
},
{
"id":"89",
"name":"Second Item",
"value":20
}
],
[
{
"id":"14",
"name":"Third one",
"value":10
}
]
]
So how do I compare each array of objects to the others in the parent array and preserve one of each duplicated or unique array of objects?
JSFiddle
you can achieve so by using function.As below. Not sure about best optimum way of doing so.
var testArray = [
[
{
"id": "65",
"name": "Some object name",
"value": 90
},
{
"id": "89",
"name": "Second Item",
"value": 20
}
],
[
{
"id": "89",
"name": "Second Item",
"value": 20
},
{
"id": "65",
"name": "Some object name",
"value": 90
}
],
[
{
"id": "14",
"name": "Third one",
"value": 10
}
]
]
function removeDuplicatesFromArray(arr){
var obj={};
var uniqueArr=[];
for(var i=0;i<arr.length;i++){
if(!obj.hasOwnProperty(arr[i])){
obj[arr[i]] = arr[i];
uniqueArr.push(arr[i]);
}
}
return uniqueArr;
}
var newArr = removeDuplicatesFromArray(testArray);
console.log(newArr);
const data = [
[
{
"id": "65",
"name": "Some object name",
"value": 90
},
{
"id": "89",
"name": "Second Item",
"value": 20
}
],
[
{
"id": "89",
"name": "Second Item",
"value": 20
},
{
"id": "65",
"name": "Some object name",
"value": 90
}
],
[
{
"id": "14",
"name": "Third one",
"value": 10
}
]
];
const temp = {};
const result = [];
data.forEach(itemArr => {
const items = itemArr.filter(item => {
const isUnique = temp[`${item.id}-${item.name}-${item.value}`] === undefined;
temp[`${item.id}-${item.name}-${item.value}`] = true;
return isUnique;
});
if (items.length !== 0)
result.push(items);
});
console.log(result);

Return unique array values from an array inside an array of objects

I can't find a similar question and I'm a bit stuck. I have the following JSON array:
[
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
I'm trying to create an array of all the unique elements in the "Attributes" property, but I'm having trouble looping through each object, and then looping through the array elements to return the unique values. I'm trying to do it with filter(), or map() preferably.
EDIT: I want an array of unique elements, so: [1,2,3].
You could do it with couple of Array methods. For example:
var result = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
// map to [ ["1", "2"], ["1", "3"], [] ]
.map(item => item.Attributes)
// flatten to [ "1", "2", "1", "3" ]
.reduce((prev, curr) => prev.concat(curr), [])
// filter unique [ "1", "2", "3" ]
.filter((item, i, arr) => arr.indexOf(item) === i)
console.log(result)
If lodash is an option, you can easily get what you want:
> _.chain(foo).map('Attributes').flatten().uniq().value()
["1", "2", "3"]
You can use Array#reduce and Array#filter methods
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}
]
console.log(
// iterate over array elements
data.reduce(function(arr, ele) {
// push the unique values to array
[].push.apply(arr,
// filter out unique value
ele.Attributes.filter(function(v) {
// check element present in array
return arr.indexOf(v) == -1;
})
);
// return the unique array
return arr;
// set initial argument as an empty array
}, [])
);
With ES6 arrow function
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}
]
console.log(
data.reduce((arr, ele) => ([].push.apply(arr, ele.Attributes.filter((v) => arr.indexOf(v) == -1)), arr), [])
);
let uniqueSet = new Set()
let a = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
for(let i=0; i<a.length; i++){
a[i].Attributes.map((x) => uniqueSet.add(x))
}
console.log([...uniqueSet])
var uniqueArr = [];
var arr = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
arr.forEach(function(obj) {
var attr = obj.Attributes;
attr.forEach(function(val){
if (uniqueArray.indexOf(val) < 0) {
uniqueArray.push(val)
}
});
})
You have answers to choose from. Just for fun: this one uses es6
"use strict";
let uniqueAttr = [];
const obj = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
obj.forEach( element =>
element.Attributes.forEach(
attr => uniqueAttr.indexOf(attr) < 0 && uniqueAttr.push(attr)
)
);
document.querySelector("#result").textContent = uniqueAttr;
<pre id="result"></pre>
Try This, It'll help to solve this problem.
var data = [{
"Name": "element1",
"Attributes": ["1", "2"]
}, {
"Name": "element2",
"Attributes": ["1", "3"]
}, {
"Name": "element3",
"Attributes": []
}];
var Attributes = [];
$.each(data, function(i, e) {
$.each(e.Attributes, function(i, e) {
Attributes.push(parseInt(e));
});
});
Attributes = $.unique(Attributes);
alert(Attributes);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
With ES6/ES2015 you can use Set and the spread operator:
const input = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
];
const output = [...new Set([].concat(...input.map(item => item.Attributes)))];
console.log(output);
Explanation (from the inside out):
input.map(item => item.Attributes) produces an array of the Attributes arrays
[].concat(...) flattens the arrays, i.e. produces an array of all the Attributes values (including duplicates)
new Set() produces a Set from the array, i.e. stores only the unique Attribute values
[...] produces an array from the Set's values, i.e. produces an array of all unique Attribute values
Building on top of #dfsq answer, you could replace the two map and reduce with a single flatMap
var result = [
{
"Name": "element1",
"Attributes": ["1", "2"]
},
{
"Name": "element2",
"Attributes": ["1","3" ]
},
{
"Name": "element3",
"Attributes": []
}
]
// map & flatten to [ "1", "2", "1", "3" ]
.flatMap(item => item.Attributes)
// filter unique [ "1", "2", "3" ]
.filter((item, i, arr) => arr.indexOf(item) === i)
console.log(result)

Categories

Resources