I have an array like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
},
{
name: 'Cancellation',
component: '/cancellation'
}
]
}]
and another like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
}
]
},
{
id: 3,
name: 'Example'
}]
My question is how is the best way to filter arrays, and remove one if have duplicated or more, even in array of children. Or the best, is like arrays merge!
Related
I have been looking around for a JavaScript method to return the index of a value but I can't seem to find one that works.
I have the following code:
let topics = [
{
id: 1,
name: 'Topic 1',
children: [
{
id: 2,
name: 'Subtopic 1.1' <---- Searching for this value
}
]
}
];
Is there a method to use on the topics variable to search through the entire object array at once for the value of Subtopic 1.1 and then return the parent index, which in this case would be 0.
There isn't a single function, but you can nest an Array.prototype.find function inside an Array.prototype.findIndex without issue to achieve what you want (findIndex to search through the parents, find to search through the children):
let topics = [{
id: 1,
name: 'Topic 1',
children: [{
id: 2,
name: 'Subtopic 1.1' // <---- Searching for this value
}]
},
{
id: 2,
name: 'Topic 6',
children: [{
id: 5,
name: 'Subtopic 1.7'
}]
},
{
id: 3,
name: 'Topic 9',
children: [{
id: 4,
name: 'Subtopic 1.192'
},
{
id: 28,
name: 'Subtopic 999'
}],
},
];
function findParentIndex(name) {
return topics.findIndex(topic => topic.children.find(child => child.name === name));
}
console.log(findParentId("Subtopic 1.192")); // 3
console.log(findParentId("Subtopic 1.1")); // 1
console.log(findParentId("Not in the list")); // -1
You can use array.findIndex()
let topics = [{
id: 1,
name: 'Topic 1',
children: [{
id: 1,name: 'Subtopic 1.2'
}, {
id: 4,name: 'Subtopic 1.4'
}, {
id: 2, name: 'Subtopic 1.1'
}]
}];
const findIndexOf = val => {
return topics[0].children.findIndex(e => e.name.trim() === val.trim())
}
console.log(findIndexOf('Subtopic 1.1'))
No.
You would iterate through children, then have a nested loop iterating through each index value. If you find a match, the incrementing variable from the parent loop is the index you want.
edit: code example
let topics = [
{
id: 1,
name: 'Topic 1',
children: [
{
id: 2,
name: 'Subtopic 1.1'
},
{
id: 2,
name: 'Subtopic 3.1'
},
{
id: 2,
name: 'Subtopic 1.1'
},
{
id: 2,
name: 'Subtopic 2.1'
},
{
id: 2,
name: 'Subtopic 1.1'
}
]
}
];
for (let i in topics[0]["children"]) {
if (topics[0]["children"][i]["name"] == "Subtopic 1.1") {
console.log(i)
}
}
I have a table with following values
[
{
id: 1,
name: "abc"
},
{
id: 2,
name: "lmn"
},
{
id: 3,
name: "xyz"
}
]
I have a query with $in as
{
id: {
$in: [ 2, 3, 1 ]
}
}
i want the output to come like so:
[
{
id: 2,
name: "lmn"
},
{
id: 3,
name: "xyz"
},
{
id: 1,
name: "abc"
}
]
But it does not come like that. Is there a way I could achieve this output?
You have to add a $sort stage, as $in does not preserve order.
There are quite a few questions on stackoverflow about this issue for example this question.
I have two objects as follows below. I would like to merge the contents of one of the array to the contents of the another array based on a property. I know that this is achievable by joining in database query but I was hoping to do the merge server side to better familiarize myself with javascript
const tasks = [
{
id: 1,
name: 'john'
items: [
{
name: 'dishes',
completed: false
}
]
},
{
id: 2,
name: 'jane'
items: [
{
name: 'sweep',
completed: true
},
{
name: 'vacuum',
completed: false
}
]
}
];
and
const progress = [
{
id: 1
items: [
{
name: 'dishes',
progression: 50
}
]
},
{
id: 2,
items: [
{
name: 'sweep',
progression: 100
},
{
name: 'vacuum',
progression: 10
}
]
}
]
How do I go about getting the object to look like below?
[
{
id: 1,
name: 'john'
items: [
{
name: 'dishes',
completed: false
progression: 50
}
]
},
{
id: 2,
name: 'jane'
items: [
{
name: 'sweep',
completed: true
progression: 100
},
{
name: 'vacuum',
completed: false
progression: 10
}
]
}
];
Cheers!
If you are sure that items will always have 1 item then below function should work for you
function merge(){
const merged = tasks.map(task=>{
let progres = progress.find(progres=>progres.id===task.id)
if(progres){
task.items = [{...task.items[0],...progres.items[0]}]
}
return task;
});
return merged;
}
I need to get the ID’s from arr1 that match names in arr2 and replace/add these name ID pairs into arr3.
arr1: {
[
id: [1234, 12345],
name: ‘one’,
},
{
id: [1270, 67812],
name: ‘two’,
},
{
id: [4970, 5170],
name: ’three’,
},
{
id: [02413, 02613],
name: ‘four’,
},
{
id: [8970],
name: ‘five’,
},
{
id: [7170, 6570, 6370],
name: ‘six’,
},
{
id: [8170, 22012, 4578, 33613],
name: ‘seven’,
},
{
id: [21812, 6170, 20412, 42812, 22012, 8170],
name: ‘eight’,
}]
arr2: [{
name: ‘one’,
}, {
name: ‘two’,
}, ]`
This would be result that needs to get pushed into a third array replacing the name elements with both names and ids.
arr3: [{
id: [1234, 12345],
name: ‘one’,
}, {
id: [1270, 67812],
name: ‘two’,
}
How should one structure Redux state when retrieving domain objects that have different search parameters.
In my application I display a list of organisations that the user filters, in a table. On the same page I will also display a smaller list of organisations that a user is part of, and perhaps in the future another small list on the same page that displays only organisations from another user.
Do i do this:
{
list_organisations: [
{ id: 1, name: 'foo1' //... },
{ id: 2, name: 'foo2' //... },
{ id: 3, name: 'foo3' //... },
{ id: 4, name: 'foo4' //... },
{ id: 5, name: 'foo5' //... },
],
user_one_organisations: [
{ id: 6, name: 'foo' //... },
{ id: 2, name: 'foo' //... },
],
user_two_organisations: [
{ id: 4, name: 'foo' //... },
{ id: 6, name: 'foo' //... },
],
}
or this:
{
users: [
{ id: 1, organisations: [7,3,8], name: 'billy' },
{ id: 2, organisations: [3,6,1], name: 'sam' },
]
organisations: [
{ id: 1, name: 'foo', //... },
{ id: 2, name: 'foo1', //... },
{ id: 3, name: 'foo2', //... },
{ id: 4, name: 'foo3', //... },
{ id: 5, name: 'foo4', //... },
{ id: 6, name: 'foo5', //... },
{ id: 7, name: 'foo6', //... },
{ id: 8, name: 'foo7', //... },
{ id: 9, name: 'foo8', //... },
],
}
If we go with option two, what do we do in the case that we need to lookup a single organisation for some purpose e.g. "check if a users email exists within an organisation" -- it all just seems so complex... especially when doing one off requests? should that even live in the redux state?
That would make it like so:
{
users: [
{ id: 1, organisations: [7,3,8], name: 'billy' },
{ id: 2, organisations: [3,6,1], name: 'sam' },
]
organisation_signup_form: {
doesUsersEmailExist: true / false / null,
}
organisations: [
{ id: 1, name: 'foo', //... },
{ id: 2, name: 'foo1', //... },
{ id: 3, name: 'foo2', //... },
{ id: 4, name: 'foo3', //... },
// ...
],
}
I'd actually recommend structuring your data in a completely different way. You want to make sure that all of your models are easy to get at so keeping them in an array can be tricky.
I'd suggest a state structure something like this:
users: {
1: { id: 1, organisations: [7,3,8], name: 'billy' },
2: { id: 2, organisations: [3,6,1], name: 'sam' },
},
userList: [1,2],
organisation_signup_form: {
doesUsersEmailExist: true / false / null,
},
organisations: {
1: { id: 1, name: 'foo', //... },
2: { id: 2, name: 'foo1', //... },
3: { id: 3, name: 'foo2', //... }
}
I got this advice from Dan on this question
check if a users email exists within an organisation
You don't have an email on the user model and it's not clear so it's quite difficult to answer that specific question.
One bit of advice I'd give is that you need to structure your state in a database kind of way but it doesn't have to be the same structure as your actual database or api endpoints.