javascript print nested array to screen - javascript

I have a data array, there are multiple objects in this array, and the data object is an array. There may be more than one object in this array. How can I copy these objects under a single array?
const test = []
const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
]
[{
"price":10,
"title": "sda"
},
{
"price":20,
"title": "asdas"
},
]
If this is the output I expect, it should be like this. how can I do that

const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
{
"price":99,
"title":"aaaaa"
}
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
];
console.log(data.map(e => e.data).flat());
// OR
console.log(data.flatMap(e => e.data));

Related

how to sort array of object in js by property i.ecategoryname' in my dataset in alphabetical order using mergesort

Here is the list of data that should be sorted in alphabetical based of categoryName
value using merger sort
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows":
{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
i can use filter and includes method to sort.. but i have to implement some types of algorithm in my project like{mergersort ,bubblesort,depthsearch etc}but for now i need to use mergesort***
The final result should be like this i have not included all the property inside the object here i think you got it ..
filteritem=[
{categoryName:"admission"...},
{cateogryName:"Binodkhatricv"..},
{categoryName:"officialdocument"...},
{categoryName:"syallabus"...}
]```
You can use sort I guess it's do want to try to do :
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows":
[{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
}];
let sorted = filteritem.sort((a, b) => (a < b ? -1 : 1))
console.log(sorted)
You need to use localeCompare to sort the string, but after toLowerCase to ignore case sensitivity.
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows": [
{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
}
]
const result = filteritem.sort((a, b) => a.categoryName.toLowerCase().localeCompare(b.categoryName.toLowerCase()))
console.log(result)

Generate dynamic payload as per nested array item click javascript

I have this JSON data:
{
"data": [
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iug",
"title": "Art"
},
"subcategories": [
{
"documentId": "c8kr7nv012vtr8vm3l8g",
"title": "Architecture"
},
{
"documentId": "c8kr7nv012vtr8vm3lag",
"title": "Dance"
},
{
"documentId": "c8kr7nv012vtr8vm3lbg",
"title": "Fashion"
}
]
},
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iqg",
"title": "Business"
},
"subcategories": [
{
"documentId": "c8kr3d7012vtr8vm3jjg",
"title": "Crypto"
},
{
"documentId": "c8kr3d7012vtr8vm3jj0",
"title": "Finance"
},
{
"documentId": "c8kr3d7012vtr8vm3jkg",
"title": "Marketing"
}
]
}
]
}
I have tagview in my mobile screen so user can select multiple subcategory at a same time by select deselect so if i choose 2 subcategory from Art and 2 from Business then expected post payload should be like below
{
"data": [
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
}
So my code is like below
const tempDefaultPayload = dataPayload.map(x => {
if (x.category) {
x.subcategories?.push(subcategory);
if (userData?.userInterests?.map(v => v.category.documentId === category)) {
const arrayNewData: any = [];
const newData: any = userData?.userInterests?.map(i =>
i.subcategories?.map(k => {
arrayNewData.push(k?.documentId);
return k?.documentId;
}),
);
x.subcategories?.push(...arrayNewData);
}
return x;
}
return x;
})
When I run above code payload is not passing correct
[
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
It is passing all selected subcategory in both array any idea how can I solve this ?
If you have the selected tag ids in the array selectedTags and your json as a variable called data:
const tempDefaultPayload = {
data: data.data.map(x => {
"category": x.category.documentId,
"subcategories": x.subcategories.map(subcat =>
subcat.documentId).filter(item => selectedTags.includes(item))
})
}
Playground example

push elements of each object inside each object inside another array

I have two arrays, one is my original one called data which consists of :
const datas = [
{
name: 'core Test',
item: [
{
name: 'test/core/core.js',
item: "item1"
}
]
},
{
name: 'users Test',
item: [
{
name: 'test/users/user.js',
item: "item2"
}
]
}
]
And i have another array called replace, which i'm trying to push each of its elements inside my original one, inside the
const replace = [
{
type: "test1",
number: "1",
},
{
type: "test2",
number: "2",
}
]
Here is my code :
const transformedData = datas.map(data => {
data.item = data.item.map(x => ({
name: x.name,
type: replace.map(y=>{return y;})
}))
return data
})
The output i get :
[
{
"name": "core Test",
"item": [
{
"name": "test/core/core.js",
"type": [
{ "type": "test1", "number": "1" },
{ "type": "test2", "number": "2" }
]
}
]
},
{
"name": "users Test",
"item": [
{
"name": "test/users/user.js",
"type": [
{ "type": "test1", "number": "1" },
{ "type": "test2", "number": "2" }
]
}
]
}
]
The output i want :
[
{
"name": "core Test",
"item": [
{
"name": "test/core/core.js",
"type": { "type": "test1", "number": "1" }
}
]
},
{
"name": "users Test",
"item": [
{
"name": "test/users/user.js",
"type": { "type": "test2", "number": "2" }
}
]
}
]
This is because you're mapping all the way through the replace array every single time for each time you're inside of a value inside of datas. Instead you want to keep track of the index with your original map so then you only have one instance each time.
Try something like:
const transformedData = datas.map((data, index) => {
data.item = data.item.map(x => ({
name: x.name,
type: replace[index]
}))
return data;
});

get records doing a match between arrays rethinkdb

I want get the id from table records that have the array match with other record from the same table's array example:
it is record of user '1'
✔ r.db('fotogena').table('users').filter({user:'1'}).pluck('pleasures')
{
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af5-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1afb-a7b1-11e7-86dc-d709d79803e4",
"432f1afc-a7b1-11e7-86dc-d709d79803e4",
"432f1afd-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b03-a7b1-11e7-86dc-d709d79803e4",
"432f1b04-a7b1-11e7-86dc-d709d79803e4",
"432f1b07-a7b1-11e7-86dc-d709d79803e4"
]
}
]
}
and i want compare each one that items(without the user '1') inside that pleasure.subCategory with others records inside the same table with the same estructure.
Table with 3 records
[
{
"date": "2017-10-03T03:58:02.651Z",
"id": "d82279a7-fbc6-40a2-99ca-39796ea57efa",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af5-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1afb-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b03-a7b1-11e7-86dc-d709d79803e4",
]
}
],
"user": "1"
},
{
"date": "2017-10-07T02:59:45.942Z",
"id": "174c0e35-da79-4ca8-b237-8ec569cc27b1",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e4",
"432f1af4-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1aff-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b04-a7b1-11e7-86dc-d709d79803e4",
]
}
],
"user": "10"
},
{
"date": "2017-10-07T02:07:13.715Z",
"id": "dd11edac-e0f5-43ac-811a-eaa78a6509c7",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e5"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e5"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e6"
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e7"
]
}
],
"user": "25"
}
]
i think that maybe can be it:
r.db('fotogena').table('users').filter({user:'1'}).pluck('pleasures').map(pleasures=>{
return //do something
})
i could solve it, later of search and search, i could created a script that show that i want get
I had that do in unique query(no joins) compare arrays with arrays using .contains() and worked very good
r.db('fotogena').table('users').filter(users=>{
return users('pleasures').contains(category=>{
return category('subCategory').contains(subCategory=>{
return r.db('fotogena').table('users').filter({user: '1'}).pluck('pleasures').contains(pleasures2=>{
return pleasures2('pleasures').contains(subCate2=>{
return subCate2('subCategory')
.contains(subCategory2=>{
return subCategory2.eq(subCategory)
})
})
})
})
}).and(users('user').ne('1'))
})

Building new JSON from existing one

I want to build an new JSON from existing one. The source has sections and rubrics that I no longer need for a listing. The new object called 'items' should have an array of the items.
The final JSON should be sorted by attribute 'name' and look like
{
"items": [
{
"id": 10000006,
"name": "Boah"
},
{
"id": 10000013,
"name": "Gut"
},
{
"id": 10000003,
"name": "Ipsum"
},
{
"id": 10000001,
"name": "Lorem"
},
{
"id": 10000005,
"name": "Lorum"
},
{
"id": 10000004,
"name": "Name"
},
{
"id": 10000002,
"name": "Stet"
}
]
}
For building the new JSON I get this source:
{
"sections": [
{
"name": "FooBar",
"rubrics": [
{
"name": "Foo",
"items": [
{
"id": 10000001,
"name": "Lorem"
},
{
"id": 10000002,
"name": "Stet"
},
{
"id": 10000003,
"name": "Ipsum"
}
]
},
{
"name": "Bar",
"items": [
{
"id": 10000004,
"name": "Name"
},
{
"id": 10000005,
"name": "Lorum"
},
{
"id": 10000006,
"name": "Boah"
}
]
}
]
},
{
"name": "BlahBloob",
"rubrics": [
{
"name": "Bla",
"items": [
{
"id": 10000013,
"name": "Gut"
}
]
},
{
"name": "Bloob",
"items": [
{
"id": 10000014,
"name": "Name"
},
{
"id": 10000015,
"name": "Lorem"
}
]
}
]
}
]
}
What do you think? How can I do this with plain JavaScript or maybe TypeScript?
Thanks for reading and have time for my question. And thanks for reply in advance.
Here you go. You just need to iterate over each rubric of each section of your source to get the items. At the end, sort your list of items by items, and you're done.
This example uses ES6 syntax, but it's easy to convert it to ES5 if needed.
function extractItems(source) {
const items = [];
for (const section of source.sections) {
for (const rubric of section.rubrics) {
items.push(...rubric.items);
}
}
items.sort((a, b) => a.name.localeCompare(b.name));
return { items };
}
A more functional approach use map and reduce to pick the rubrics and merge them.
data.sections
.map(section => section.rubrics) // get rubrics
.reduce((a, b) => a.concat(b)) // merge rubrics
.map(rubric => rubric.items) // get items from each rubric
.reduce((a, b) => a.concat(b)) // merge items
.sort((a, b) => a.name.localeCompare(b.name)); // sort
function(oldObj) {
var newObj = {
"items": []
};
oldObj.sections.forEach(function(section) {
section.rubrics.forEach(function(rubric) {
rubric.items.forEach(function(item) {
newObj.items.push(item);
});
});
});
newObj.items = newObj.items.sort(function(a, b) {
if (a.name < b.name) { return -1; }
if (a.name > b.name) { return 1; }
return 0;
});
return newObj;
}
And simply use JSON.parse() and JSON.stringify() to convert JSON to and from objects.
It might help you
var data ={
"sections": [
{
"name": "FooBar",
"rubrics": [{"name": "Foo", "items": [{"id": 10000001,"name": "Lorem"}, {"id": 10000002,"name": "Stet"}, {"id": 10000003,"name": "Ipsum"}]
}, {
"name": "Bar",
"items": [{
"id": 10000004,
"name": "Name"
}, {
"id": 10000005,
"name": "Lorum"
}, {
"id": 10000006,
"name": "Boah"
}]
}]
}, {
"name": "BlahBloob",
"rubrics": [{
"name": "Bla",
"items": [{
"id": 10000013,
"name": "Gut"
}]
}, {
"name": "Bloob",
"items": [{
"id": 10000014,
"name": "Name"
}, {
"id": 10000015,
"name": "Lorem"
}]
}]
}]
};
var itemObj = {};
var itemArr = [];
var sections = data.sections;
for(var i=0;i<sections.length;i++)
{
for(var j=0;j<sections[i].rubrics.length;j++){
for(var k=0;k<sections[i].rubrics[j].items.length;k++){
var itemObj;
itemObj['id'] = sections[i].rubrics[j].items[k].id;
itemObj['name'] = sections[i].rubrics[j].items[k].name;
itemArr.push(itemObj);
}
}
}
var finalObj = {"items":itemArr};
console.log(finalObj);
JSFiddle

Categories

Resources