What is the best way to combine two immutable lists? - merge

I have two lists and i'm trying to combine them to a new list so that the existing ids are updated and the new ones are added to list and after that sorted by the id. Is there a better or more efficient way to do this?
// Original list
const list = Immutable.List([
{ id: 1, name: 'List Item 1' },
{ id: 2, name: 'List Item 2' },
{ id: 3, name: 'List Item 3' },
]);
// One updated item and two new items
const newList = Immutable.List([
{ id: 2, name: 'Updated List Item 2' },
{ id: 4, name: 'New List Item 4' },
{ id: 5, name: 'New List Item 5' },
]);
// Get updated ids
const ids = newList.map((item) => item.id);
// Filter out updated ids from orignial list
const filteredList = list.filterNot(item => ids.includes(item.id));
// Concat and sort by id
const concatList = newList
.concat(filteredList)
.sortBy(item => item.id);
console.log(concatList.toJS());
/* Outputs as desired
[
{ id: 1, name: "List Item 1" },
{ id: 2, name: "Updated List Item 2" },
{ id: 3, name: "List Item 3" },
{ id: 4, name: "New List Item 4" },
{ id: 5, name: "New List Item 5" }
]
*/

This is how I would do it, using reduce and merge:
function reduceToMap(result, item) { return result.set(item.id, item) }
const list = Immutable.List([
{ id: 1, name: 'List Item 1' },
{ id: 2, name: 'List Item 2' },
{ id: 3, name: 'List Item 3' },
]).reduce(reduceToMap, Immutable.Map());
// One updated item and two new items
const newList = Immutable.List([
{ id: 2, name: 'Updated List Item 2' },
{ id: 4, name: 'New List Item 4' },
{ id: 5, name: 'New List Item 5' },
]).reduce(reduceToMap, Immutable.Map());
console.log(...list.merge(newList).values())
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>

Related

Can't remove nested elements from object

I have this table here:
Table: User
{
id: 1,
name: 'user 1',
superPowers: [
{
id: 1,
map: {
status: 'offline'
}
},
{
id: 2,
map: {
status: 'online'
}
},
{
id: 3,
map: {
status: 'online'
}
}
]
}
And I would like to return an object with the user information only containing superpowers that have the map in the online mode. I built this query:
await getRepository(User).createQueryBuilder('user')
.leftJoinAndSelect('user.superPowers', 'superPowers')
.leftJoinAndSelect('superPowers.map', 'map')
.where('map.status = status', {status: EnumStatusMap.ONLINE})
.andWhere('user.id = :id', {id: 1}).getOne();
But this query returns me all the supowerPowers, including the ones where the map status is offline
What am I doing wrong?

Sort JSON list in Flutter [duplicate]

This question already has answers here:
Sort a list of objects in Flutter (Dart) by property value
(12 answers)
Closed 3 years ago.
I need to sort a list in Flutter, that's an object List, I have a model and that model has a property named as isFeatured, I need that all the elements with isFeatured as true be in the first positions of the List.
I mean I could have something like:
[
{
id: 1,
name: 'Test',
isFeatured: false,
},
{
id: 2,
name: 'Test 3',
isFeatured: true,
},
{
id: 3,
name: 'Test 5',
isFeatured: false,
},
{
id: 4,
name: 'Test 34',
isFeatured: true,
}
]
And the elements with isFeatured as true should be in the first position.
you can use sort method with custom comparator for bool
myList.sort((a, b) => (a.isFeature ? 1 : 0) - (b.isFeature ? 1 : 0));
Use List.sort
var myList = [
{
'id': 1,
'name': 'Test',
'isFeatured': false,
},
{
'id': 2,
'name': 'Test 3',
'isFeatured': true,
},
{
'id': 3,
'name': 'Test 5',
'isFeatured': false,
},
{
'id': 4,
'name': 'Test 34',
'isFeatured': true,
}
];
myList.sort((a, b) => (b['isFeatured']?1:0).compareTo(a['isFeatured']?1:0));

Traverse through an Array to obtain nested object

I am trying to traverse through my array to get a specific object nested inside of it.
Some objects contain a children property, which should be traversed until a matching object is found.
Here's some example data, I am trying to obtain the object with id as 4
const items = [{
id: 1,
title: 'Title for Item 1'
},
{
id: 2,
title: 'Title for Item 2',
children: [
{
id: 3,
title: "Title for Item 3",
children: [
{
id: 4,
title: "Title for Item 4",
}
]
}
]
},
]
I've written some traversal code but it returns undefined.
const items = [{
id: 1,
title: 'Title for Item 1'
},
{
id: 2,
title: 'Title for Item 2',
children: [
{
id: 3,
title: "Title for Item 3",
children: [
{
id: 4,
title: "Title for Item 4",
}
]
}
]
},
]
const getItem = (items) => {
if (!items) return;
const item = items && items.find(i => i.id === 4);
if (!item) {
items.forEach(i => {
return getItem(i.children)
})
// This is where undefined is returned
} else {
console.log({
item
}) // Prints the correct object.
return item;
}
};
const s = getItem(items); // undefined
document.querySelector('#foo').textContent = s ? s : 'undefined';
<div id="foo"></div>
At least two issues explain why it does not work:
A return statement in a forEach callback will return the returned value to nowhere. Nothing happens with it.
The result of the recursive call is not checked. It needs to be checked to see if it is defined. Depending on that you can decide whether to continue the loop or exit from it.
Replace that forEach with a for...of loop so you can return "out of it", but only do that when you have a match, otherwise you need to continue the loop:
for (const item of items) {
const match = getItem(item.children);
if (match) return match;
}
Note that in your snippet you should not set the textContent to the return value, as that is an object and will get converted to the string "[Object object]". You could for instance just grab the title string and put that in textContent:
const items = [{
id: 1,
title: 'Title for Item 1'
},
{
id: 2,
title: 'Title for Item 2',
children: [
{
id: 3,
title: "Title for Item 3",
children: [
{
id: 4,
title: "Title for Item 4",
}
]
}
]
},
]
const getItem = (items) => {
if (!items) return;
const item = items && items.find(i => i.id === 4);
if (!item) {
for (const item of items) {
const match = getItem(item.children);
if (match) return match;
}
} else {
console.log({
item
}) // Prints the correct object.
return item;
}
};
const s = getItem(items); // undefined
document.querySelector('#foo').textContent = s ? s.title : 'undefined';
<div id="foo"></div>

How to iterate over multiple arrays without nested observables

I must iterate over array, find correspondent objects in other array an merge the result in a object.
Assume I have three arrays
var users = [
{ name: "A", type: 2, level: 1 },
{ name: "B", type: 1, level: 2 }
]
var types = [
{ description: "Type 1", id: 1 },
{ description: "Type 2", id: 2 }
]
var levels = [
{ description: "Level 1", id: 1 },
{ description: "Level 2", id: 1 }
]
I want to have following result:
var users = [
{ name: "A", type: 2, level: 1, levelDescription: "Level 1", typeDescription: "Type 2" },
{ name: "B", type: 1, level: 2, levelDescription: "Level 2", typeDescription: "Type 1" }
]
I know I can achieve it like that
var usersObservable = RX.Observable.fromArray(users);
var typesObservable = Rx.Observable.fromArray(types);
var levelsOBservable = Rx.Observable.fromArray(levels);
var uiUsers= [];// not really needed because I will use the same users array again.
usersObservable.map(function(user) {
typesObservable.filter(function(type) {
return type.id == user.type;
}).subscribeOnNext(function(userType) {
user.typeDescription = userType.description;
});
return user;
}).map(function(user) {
levelsOBservable.filter(function(level) {
return level.id == user.levelId;
}).subscribeOnNext(function(level) {
user.levelDescription = level.description;
});
return user;
})
.subscribeOnNext(function(user) {
uiUsers.push(user);
})
I would like to have a solution without nested Observables.
Thanks.
I am not sure why you are using Rx at all for this problem. You have data in space (i.e. arrays), not data over time (i.e. an observable sequence). But you force these arrays into Rx to then create a very complicated solution.
I think you are looking for something like the answer here https://stackoverflow.com/a/17500836/393615 where you would join the source array types. In your case you just "inner-join" twice to combine all three data sets.
You can archive this by using the switchMap operator that combines the result of a filtered stream with the latest value of the original stream and uses a projection function to merge the results into a single object. This can be generalised in your example such that you can use a generic higher order function in both cases. See fiddle.
Full code (ES2015, RxJS5):
const users = [
{ name: "A", type: 2, level: 1 },
{ name: "B", type: 1, level: 2 }
];
const types = [
{ description: "Type 1", id: 1 },
{ description: "Type 2", id: 2 }
];
const levels = [
{ description: "Level 1", id: 1 },
{ description: "Level 2", id: 2 }
];
const users$ = Rx.Observable.from(users);
const types$ = Rx.Observable.from(types);
const levels$ = Rx.Observable.from(levels);
function join(s$, sourceProperty, targetProperty, streamProperty) {
return function(initObj) {
const stream$ = s$.filter(x => x.id === initObj[sourceProperty]);
return Rx.Observable.combineLatest(
Rx.Observable.of(initObj),
stream$,
(obj, streamObj) => {
const prop = streamObj[streamProperty];
return Object.assign({}, obj, { [targetProperty]: prop });
}
);
};
}
users$
.switchMap(join(types$, 'type', 'typeDescription', 'description'))
.switchMap(join(levels$, 'level', 'levelDescription', 'description'))
.subscribe(x => console.log(x));

How to redirect to a nested dynamic segments in EMberjs

If we have several books,each books contains several chapters and each chapter contains several pages.
In an App,
when user navigate to
"/home"
list all the books,clicking a book(eg:book_1) will directly "linkTo"
"/book_1/chapter_1/page_1"
show the content of "chapter_1/page_1" within the selected book.
I am now trying to use the "redirect" hook,but I am confused with these:
1,How to sent muti params to a redirect hook;
2,How to update the URL correctly after redirecting?
3,What is the "Ember way" to do this?
_BOOK MODEL__
App.Book = DS.Model.extend({
name: DS.attr('string'),
chapters: DS.hasMany('chapter',{async:true}),
color: DS.attr('')
});
App.Chapter = DS.Model.extend({
name: DS.attr('string'),
author: DS.attr(''),
pages: DS.hasMany('page',{async:true})
});
App.Page = DS.Model.extend({
name: DS.attr('string'),
});
App.Book.FIXTURES = [
{
id: 1,
name: 'book1',
color: 'red',
chapters: [1,2]
},
{
id: 2,
name: 'book2',
color: 'blue',
chapters: [3,4]
}
];
App.Chapter.FIXTURES = [
{
id: 1,
name: 'Chapter1',
author: 'jhon',
pages:[1,2]
},
{
id: 2,
name: 'Chapter2',
author: 'jhon',
pages:[3,4]
},
{
id: 3,
name: 'Chapter3',
author: 'peter',
pages:[5,6]
},
{
id: 4,
name: 'Chapter4',
author: 'tom',
pages:[7,8]
}
];
App.Page.FIXTURES = [
{
id: 1,
name: 'page1',
},
{
id: 2,
name: 'page2',
},
{
id: 3,
name: 'page3',
},
{
id: 4,
name: 'page4',
},
{
id: 5,
name: 'page5',
},
{
id: 6,
name: 'page6',
},
{
id: 7,
name: 'page7',
},
{
id: 8,
name: 'page8',
}
];
redirect is deprecated, use the afterModel hook. I'm sending in ids of 1 and 1 to the chapter and page route, which should hit their model hooks. How is your data separated, does the book model have all the necessary info?
http://emberjs.jsbin.com/uDiLIkij/6/edit
afterModel: function(model, transition){
console.log(transition.targetName);
if(transition.targetName=='book.index'){
var self = this;
model.get('chapters').then(function(chs){
var ch = chs.get('firstObject');
ch.get('pages').then(function(pages){
var page = pages.get('firstObject');
self.transitionTo('page', model, ch, page);
});
});
transition.abort();
}
},