I have an array that I like to group by the ID, but pushing all the different elements in the arrays in the result.
let data = [
{ id: "1", elements: ["a","b"], files: [] },
{ id: "1", elements: ["a","b"], files: [] },
{ id: "2", elements: ["a","b","c"],
files: [
{ name: "test1",
extension: "pdf"
},
{ name: "test2",
extension: "pdf"
}
]
},
{ id: "2", elements: ["a","b","c"],
files: [
{ name: "test3",
extension: "png"
},
{ name: "test4",
extension: "png"
},
{ name: "test5",
extension: "pdf"
}
]
},
{ id: "2", elements: ["a","b","c"], files: []
}
];
I want a result like
result =
[
{
"id": "1", "elements": [ "a", "b" ], "files": [] },
{
"id": "2", "elements": [ "a", "b", "c" ],
"files": [
{ "name": "test1", "extension": "pdf" },
{ "name": "test2", "extension": "pdf" },
{ "name": "test3", "extension": "png" },
{ "name": "test4", "extension": "png" },
{ "name": "test5", "extension": "pdf" },
]
}
]
How it is possible with lodash or any other process getting the desire output
You can use Array.reduce() to group the items by id.
This creates an object with a property for each id, we can then use Object.values() to get the result as an array.
For the elements array, we'll also use a Set to ensure we don't duplicate elements. We only want ['a','b'] rather than ['a','b','a','b'] for example.
let data = [ { id: "1", elements: ["a","b"], files: [] }, { id: "1", elements: ["a","b"], files: [] }, { id: "2", elements: ["a","b","c"], files: [ { name: "test1", extension: "pdf" }, { name: "test2", extension: "pdf" } ] }, { id: "2", elements: ["a","b","c"], files: [ { name: "test3", extension: "png" }, { name: "test4", extension: "png" }, { name: "test5", extension: "pdf" } ] }, { id: "2", elements: ["a","b","c"], files: [] } ];
const result = Object.values(data.reduce((acc, { id, elements, files }) => {
acc[id] = acc[id] || { id, elements: [], files: []};
// Use a Set to avoid duplicates...
acc[id].elements = [...new Set([...elements, ...acc[id].elements])];
acc[id].files.push(...files);
return acc;
}, {}));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }
Related
I'm using mongoose.
Just like 'Mysql Join',
I want to get the data that merge the parents and children collection below.
Parent
[
{
type: "A",
results: [
{
"id": 111111
},
{
"id": 222222
}
]
},
{
type: "B",
results: [
{
"id": 333333
},
{
"id": 444444
}
]
}
]
Child
[
{
dataId: 111111,
results: [
{ "status": { key: "value" } }
]
},
{
dataId: 222222,
results: [
{ "status": { key: "value" } }
]
},
{
dataId: 333333,
results: [
{ "status": { key: "value" } }
]
},
{
dataId: 444444,
results: [
{ "status": { key: "value" } }
]
},
]
Because ObjectId cannot be inserted,
it seems that it cannot be processed using the population method.
I want to merge the two data like Join in MySQL.
like below
Parent.find()
[
{
type: "A",
results: [
{
"id": 111111,
results: [
{ "status": { key: "value" } }
]
},
{
"id": 222222,
results: [
{ "status": { key: "value" } }
]
}
]
},
{
type: "B",
results: [
{
"id": 333333,
results: [
{ "status": { key: "value" } }
]
},
{
"id": 444444,
results: [
{ "status": { key: "value" } }
]
}
]
}
]
You can use this query:
$unwind to get every result from the array to merge with the Child dataid.
$lookup which is the "join" in mongodb. Here query is mergin field id into results from the Parent with dataId from the Child.
$unwind again because $lookup creates an array.
$group to group the values according the id.
$project (this stage is optional) to not shown fields you don't want.
yourParentModel.aggregate([
{
"$unwind": "$results"
},
{
"$lookup": {
"from": "Child",
"localField": "results.id",
"foreignField": "dataId",
"as": "child_results"
}
},
{
"$unwind": "$child_results"
},
{
"$group": {
"_id": "$_id",
"type": {
"$first": "$type"
},
results: {
"$push": "$child_results"
}
}
},
{
"$project": {
"_id": 0,
"results._id": 0
}
}
])
Example here
you could do this from javascript directly
first you get the parent from mongoDB
( use mongoose find method)
let parents = [
{
type: "A",
results: [
{
id: 111111,
},
{
id: 222222,
},
],
},
{
type: "B",
results: [
{
id: 333333,
},
{
id: 444444,
},
],
},
];
then you get the child from the database
let children = [
{
dataId: 111111,
results: [{ status: { key: "value" } }],
},
{
dataId: 222222,
results: [{ status: { key: "value" } }],
},
{
dataId: 333333,
results: [{ status: { key: "value" } }],
},
{
dataId: 444444,
results: [{ status: { key: "value" } }],
},
];
and the treatement to merge the parents with the children will be something like this
for (let parent of parents) {
for (let objectId of parent.results) {
for (let child of children) {
if (child.dataId === objectId.id) {
objectId.results = child.results;
break;
}
}
}
}
How to return name and id property value of all arrays? The idea is to make a single map of all these arrays and return the id and name?
Something like this
filters.[key].map((option, index) => (
<ItemFilter key={index}>{option}</ItemFilter>
))
I have this array object
filters: {
"services": [
{
"id": "1b975589-7111-46a4-b433-d0e3c0d7c08c",
"name": "Bank"
},
{
"id": "91d4637e-a17f-4b31-8675-c041fe06e2ad",
"name": "Income"
}
],
"accountTypes": [
{
"id": "1f34205b-2e5a-430e-982c-5673cbdb3a68",
"name": "Digital Account"
}
],
"channels": [
{
"id": "875f8350-073e-4a20-be20-38482a86892b",
"name": "Chat"
}
]
}
You can use flatMap or flat to achieve the desired result.
Object.values(obj.filters).flatMap(v => v)
or
Object.values(obj.filters).flat()
const obj = {
filters: {
services: [
{
id: "1b975589-7111-46a4-b433-d0e3c0d7c08c",
name: "Bank",
},
{
id: "91d4637e-a17f-4b31-8675-c041fe06e2ad",
name: "Income",
},
],
accountTypes: [
{
id: "1f34205b-2e5a-430e-982c-5673cbdb3a68",
name: "Digital Account",
},
],
channels: [
{
id: "875f8350-073e-4a20-be20-38482a86892b",
name: "Chat",
},
],
},
};
const result = Object.values(obj.filters).flatMap(v => v);
console.log(result);
If option is referring to name in your example code it could look something like this:
Object.values(
{
filters: {
services: [
{
id: "1b975589-7111-46a4-b433-d0e3c0d7c08c",
name: "Bank",
},
{
id: "91d4637e-a17f-4b31-8675-c041fe06e2ad",
name: "Income",
},
],
accountTypes: [
{
id: "1f34205b-2e5a-430e-982c-5673cbdb3a68",
name: "Digital Account",
},
],
channels: [
{
id: "875f8350-073e-4a20-be20-38482a86892b",
name: "Chat",
},
],
},
}.filters
)
.flat()
.map(({ name, index }) => <ItemFilter key={index}>{name}</ItemFilter>);
I have an array of object that defines file and directory structure. It will vary according to reality, which may be more complicated.
Here is an example :
For example this root folder contains "folder_id": "F1" and "folder_id": "F2", and it will continue to divide ...
const folders = [
{
folder_id: "F1",
name: "Test Folder 1",
files: [
{
file_id: "File1",
name: "Whiteboard-Jun 3rd 2020, 4:56 pm"
}
],
folders: [
{
folder_id: "Folder1",
name: "Sub Folder",
files: [
{
file_id: "File1-1",
name: "New Microsoft Word Document.docx"
}
],
folders: [
{
folder_id: "Folder1-1",
name: "Folder Grade 2",
files: [
{
file_id: "File1-1-1",
name: "Test file in Folder Grade 3"
}
],
folders: [
{
folder_id: "Folder1-1-1",
name: "Folder Grade 3",
files: [],
folders: []
}
]
}
]
}
]
},
{
folder_id: "F2",
name: "Test Folder 2",
files: [
{
file_id: "File2",
name: "buildcode.png"
}
],
folders: [
{
folder_id: "Folder2",
name: "Sub folder 1",
files: [
{
file_id: "File2-1",
name: "repo.png"
}
],
folders: []
}
]
}
];
I want the output to look like this:
{
my-root-id:{
childrenIds:[
0: "F1",
1: "F2"
],
id: "my-root-id"
},
F1:{
childrenIds:[
0: "File1",
1: "Folder1"
],
parentId: "my-root-id",
id: "F1"
},
F2:{
childrenIds:[
0: "File2",
1: "Folder2"
],
parentId: "my-root-id",
id: "F2"
},
File1:{
parentId: "File1",
id: "F1"
},
Folder1:{
childrenIds:[
0: "File1-1",
1: "Folder1-1"
],
parentId: "F1",
id: "Folder1"
},
Folder1-1:{
childrenIds:[
0: "File1-1-1",
1: "Folder1-1-1"
],
parentId: "Folder1",
id: "Folder1-1"
},
Folder2:{
childrenIds:[
0: "File2-1",
],
parentId: "F2",
id: "Folder2"
},
File2-1:{
parentId: "Folder2",
id: "File2-1"
}
}
The output above describes the relationship of each folder and file in the entire directory tree.
They need information like :
childrenIds: the folders and files it contains
parentId: Its parent directory
id: id by itself
How to do it. thank you
Don't know this actually works for you or not but output as you look for
let data = {
folders: [
{
folder_id: "F1",
name: "Test Folder 1",
files: [
{
file_id: "File1",
name: "Whiteboard-Jun 3rd 2020, 4:56 pm",
},
],
folders: [
{
folder_id: "Folder1",
name: "Sub Folder",
files: [
{
file_id: "File1-1",
name: "New Microsoft Word Document.docx",
},
],
folders: [
{
folder_id: "Folder1-1",
name: "Folder Grade 2",
files: [
{
file_id: "File1-1-1",
name: "Test file in Folder Grade 3",
},
],
folders: [
{
folder_id: "Folder1-1-1",
name: "Folder Grade 3",
files: [],
folders: [],
},
],
},
],
},
],
},
{
folder_id: "F2",
name: "Test Folder 2",
files: [
{
file_id: "File2",
name: "buildcode.png",
},
],
folders: [
{
folder_id: "Folder2",
name: "Sub folder 1",
files: [
{
file_id: "File2-1",
name: "repo.png",
},
],
folders: [],
},
],
},
],
};
const mapper = (folders, parentId) => {
let obj = {};
if (folders.length) {
folders.forEach((f) => {
if (f.folders) {
let _c = {
[f.folder_id]: {
childrenIds: f.files.map((_f) => _f.file_id),
...(parentId && { parentId }),
id: f.folder_id,
},
};
let _files = f.files.map((_f) => {
return {
[_f.file_id]: { ...(parentId && { parentId }), id: _f.file_id },
};
});
obj = { ...obj, ..._files["0"], ..._c, ...mapper(f.folders, f.folder_id) };
}
});
}
return obj;
};
console.log(mapper(data.folders));
I have a JSON with lots of empty content:
{
"items": [
{
"category": "login",
"fields": [
{
"label": "Name",
"value": "",
},
{
"label": "E-Mail",
"value": "",
},
{
"label": "Password",
"value": "123456",
},
{
"label": "Website",
"fields": [
{
"label": "Name X",
"value": ""
},
{
"label": "Name Y",
"value": "another one"
},…
]
},…
]
},…
]
}
The nesting goes several levels deeper. This shows only the first level. I want to delete all elements of "fields" (or whatever the array's key in deeper levels is), where their "value" is empty.
{
"items": [
{
"category": "login",
"fields": [
{
"label": "Password",
"value": "123456",
},
{
"label": "Website",
"fields": [
{
"label": "Name Y",
"value": "another one"
},…
]
},…
]
},…
]
}
How can I do this in Javascript?
Well, I found a way to iterate through the JSON object:
function remove(jsondata) {
for (let i in jsondata) {
if (jsondata[i].value != undefined && jsondata[i].value == '') {
delete jsondata[i];
}
else if (typeof jsondata[i] === "object") remove(jsondata[i]);
}
}
Not sure, if it's the most elegant way, but it works so far.
use filter method,you could get a filtered array
it returned Boolean. if value exist,it will be true
var list=JSON.parse(data)
list.items=list.items.map(val=>{
val.fields=val.fields.filter(v=>v.value})
return val
})
We use object-scan for many data processing tasks. It's powerful once you wrap your head around it. Here is how you could answer your questions
// const objectScan = require('object-scan');
const prune = (input) => objectScan(['**[*].value'], {
rtn: 'count',
filterFn: ({ gparent, gproperty, value }) => {
if (value === '') {
gparent.splice(gproperty, 1);
return true;
}
return false;
}
})(input);
const obj = { items: [{ category: 'login', fields: [{ label: 'Name', value: '' }, { label: 'E-Mail', value: '' }, { label: 'Password', value: '123456' }, { label: 'Website', fields: [{ label: 'Name X', value: '' }, { label: 'Name Y', value: 'another one' }] }] }] };
console.log(prune(obj)); // return count of pruned entries
// => 3
console.log(obj);
// => { items: [ { category: 'login', fields: [ { label: 'Password', value: '123456' }, { label: 'Website', fields: [ { label: 'Name Y', value: 'another one' } ] } ] } ] }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#16.0.0"></script>
Disclaimer: I'm the author of object-scan
I have an array of nested objects, that i want to keep the objects in items that have the id equal to one in the filter list that i have as reference.
const data = [{
"otherStuff": "otherB",
"items": {
"item1": [{
"id": "id1",
"info": "info1"
},
{
"id": "id2",
"info": "info22"
}
],
"item20": [{
"id": "id3",
"info": "info5"
}],
"item5": [{
"id": "id4",
"info": "info6"
},
{
"id": "id5",
"info": "info7"
}
]
}
}, {
"otherStuff": "otherA",
"items": {
"item1": [{
"id": "id1",
"info": "info10000"
},
{
"id": "id2",
"info": "info220000"
}
],
"item20": [{
"id": "id3",
"info": "info5000"
}],
"item5": [{
"id": "id4",
"info": "info60000"
},
{
"id": "id5",
"info": "info7000"
}
]
}
}];
const keep = ['id4', 'id2'];
keep.forEach(function(val) {
data.forEach(function(entry, index){
const entrySelected = [];
Object.keys(entry.items).forEach(item => {
var match = entry.items[item].find(obj => obj.id === val);
if (match) {
entrySelected.push(match)
}
});
data[index].items = entrySelected;
});
})
console.log(data)
I am getting an error right now,
but the idea is to get the following output:
[
{
"otherStuff": "otherB",
"items": [
{
"id": "id2",
"info": "info22"
},
{
"id": "id4",
"info": "info6"
}
]
},
{
"otherStuff": "otherA",
"items": [
{
"id": "id2",
"info": "info220000"
},
{
"id": "id4",
"info": "info60000"
}
]
}
]
how can i achieve this?
You could iterate the values of the objects and then reduce the wanted items for a new array. Later assing this to items.
var data = [{ otherStuff: "otherB", items: { item1: [{ id: "id1", info: "info1" }, { id: "id2", info: "info22" }], item20: [{ id: "id3", info: "info5" }], item5: [{ id: "id4", info: "info6" }, { id: "id5", info: "info7" }] } }, { otherStuff: "otherA", items: { item1: [{ id: "id1", info: "info10000" }, { id: "id2", info: "info220000" }], item20: [{ id: "id3", info: "info5000" }], item5: [{ id: "id4", info: "info60000" }, { id: "id5", info: "info7000" }] } }],
keep = ['id4', 'id2'];
data.forEach(o =>
o.items = Object
.keys(o.items)
.reduce((r, k) => o
.items[k]
.reduce((s, p) => s.concat(keep.includes(p.id) ? p : []), r), []));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
With a filter for arrays.
var data = [{ otherStuff: "otherB", items: { item1: [{ id: "id1", info: "info1" }, { id: "id2", info: "info22" }], item20: [{ id: "id3", info: "info5" }], item5: [{ id: "id4", info: "info6" }, { id: "id5", info: "info7" }] } }, { otherStuff: "otherA", items: { item1: [{ id: "id1", info: "info10000" }, { id: "id2", info: "info220000" }], item20: [{ id: "id3", info: "info5000" }], item5: [{ id: "id4", info: "info60000" }, { id: "id5", info: "info7000" }] } }],
keep = ['id4', 'id2'];
data.forEach(o =>
o.items = Object
.keys(o.items)
.filter(k => Array.isArray(o.items[k]))
.reduce((r, k) => o
.items[k]
.reduce((s, p) => s.concat(keep.includes(p.id) ? p : []), r), []));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }