Looping through data structure in coffeescript - coffeescript

I have the following data structure:
{
data: [
{
id: 5,
name: 'ItemOne'
},
{
id: 14,
name: 'ItemTwo'
},
{
id: 15,
name: 'ItemThree'
}
]
}
And I want to loop through it, but I'm struggling. I've tried:
for result,value of results
console.log results
item = 0
message = "Component: " + value[item]['name'] + " Status: " + value[item]['status']
output.push message
item++
But it only return one result. I'm obviously miles off, but what do I need to do?

You should iterate through .data. Here is a fully working example: http://jsfiddle.net/hd4ru1kf/5/
Originally in the question there was also a problem with the data notation
You can benefit from learning the CoffeeScript JSON notation here.
kids =
brother:
name: "Max"
age: 11
sister:
name: "Ida"
age: 9
Compiles to:
kids = {
brother: {
name: "Max",
age: 11
},
sister: {
name: "Ida",
age: 9
}
};
In your example you have , characters that are invalid and the whole JSON structure is way off. You have also nested the JSON awkwardly, perhaps you don't want that? Instead I think this is the working version you are looking for (you can use http://coffeescript.org/ to check it out):
data:
[
{
id: 1
name: "Item one"
}
{
id: 2
name: "Item two"
}
{
id: 3
name: "Item three"
}
]

Related

Filter documents that do not have a property set - Prisma

I have this example data:
const example = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 4,
name: "Markus",
age: 24
},
{
productId: 8,
name: "Cecilia",
age: 18
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(example)
I have a 'User' model in prisma. So what I must achieve is to filter all those users that do not have a defined productId and also filter by productId.
For example, I want to filter all users that have product Id 1 and also all users that do not have a productId defined
I have tried with the following filter
this.prisma.users.findMany({
where: {
OR: [
{ productId: undefined },
{ productId: 1 }
]
}
})
This query gets me all the users that have '1' as the value of their 'productId' but it does not bring me the users that do not have a defined productId.
The expected result of the above query would be:
const result = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(result)
I only get the users 'Husdady' and 'Anton'
const resultObtained = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(resultObtained)
How I can resolve this problem?

Is Javascript's Array.sort an example of Inversion of Control?

In Javascript's sort method of an Array, I can pass a customized sorting function. Example:
const data = [ { name: "Tom", age: 10 },
{ name: "Dick", age: 9 },
{ name: "Harry", age: 12 }
];
function mySort(A, B) {
return A.age - B.age;
}
data.sort(mySort);
console.log(data);
Is such a paradigm considered inversion of control?

MongoDB : projection matches all element corresponding to query

Supposing the following data format
{
_id: "1234",
tag: "MyTag",
members: [{
name: "James", age: 54
}, {
name: "John", age: 22
}, {
name: "Eric", age: 36
}],
},
{
_id: "7896",
tag: "MyTag2",
members: [{
name: "Philip", age: 6
}, {
name: "Mark", age: 14
}, {
name: "Maya", age: 64
}],
}
How can I request all the person over 30 to get that result
{
_id: "1234",
tag: "MyTag",
members: [{
name: "James", age: 54
}, {
name: "Eric", age: 36
}],
},
{
_id: "7896",
tag: "MyTag2",
members: [{
name: "Maya", age: 64
}],
}
Basically this request
db.MyDB.find({'members':{'$elemMatch':{'age':{'$gt':30}}}}, {'tag:1', 'members.$':1})
but where the .$ operator does not return only the first or each list
(I'm using python api but I think the question applies more generally)
Thanks a lot!
The positional operator $ and the $elemMatch projection operator both only match the first element that matches the query.
If you want to get all elements that match, use aggregation and $filter the array.

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));

In Sails.js v0.10 after populating data as a value i get [Getter/Setter]

After populating data i get result not as expected.
Example:
//User.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
schools: { collection: 'School', via: 'id' },
name: { type: 'String', trim: true, required: true }
}
}
And this is my School.js
//School.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
name: { type: 'String', trim: true, required: true },
}
}
My User entity data looks like this:
//User document in MongoDB
{
_id: 1,
name: "Foo",
schools: [1,2,3]
}
My School entity data looks like this:
//School documents in MongoDB
{
_id: 1,
name: "School 1"
}
{
_id: 2,
name: "School 2"
}
{
_id: 3,
name: "School 3"
}
Now i want to populate the schools. I do it like this:
User.find().populate("schools").exec(function(err, res){ console.log(res[0]) });
And this is what i get as a result:
{
schools: [Getter/Setter],
name: "Foo",
id: 1
}
Expected:
{
schools: [
{id: 1, name: "School 1"},
{id: 2, name: "School 2"},
{id: 3, name: "School 3"}
],
name: "Foo",
id: 1
}
How can i get expected results?
I use MongoDB as data storage.
Versions:
Sails.js: v0.10.0-rc5
Waterline: v0.10.0-rc7
sails-mongo: 0.10.0-rc2
Those are the expected results when using console.log! If you want to see the expanded object, do:
console.log(res[0].toObject());