Angular 5 - Dynamically add items to an array - arrays

I have these two functions:
cart() which is an array object (this information is static)
cartItems which is an array object (this information is dynamic)
Inside the cart there can be several cartItems. How do I push different items into this cart so my final result would look like this:
JSON:
"cart": [
{
"id": "1",
"cartItem": [
{
"id": "cart Item 1",
"action": "New",
"quantity": 1,
},
{
"id": "cart Item 2",
"action": "New",
"quantity": 1,
}
}
]
FUNCTIONS:
private cart(): CaCartItemsModel{
return new CaCartItemsModel(
"1",
"Cambio",
"TV"
);
private cartItems(): CaCartItemModel{
return new CaCartItemModel(
"cart Item 1/cart Item 2",
"New",
1
);
}

I managed to resolve what I needed in the following way:
private createCartItems(cancellationModel: CancellationModel) {
this.request.caAcProductOrderModel.caCartItems.getAllCartItems()
.push(this.setCartItems());
cancellationModel.channels.forEach(channel => {
this.request.caAcProductOrderModel.caCartItems.getAllCartItems()
.forEach(caCartItem => {
caCartItem.caCartItem.getAllCartItem()
.push(this.setCaCartItemModel(
channel.id,
channel.action,
channel.name,
"",
true
));
caCartItem.caCartItem.getAllCartItem()
.forEach(cartItem => {
cartItem.caProduct.caProductRelationship.getAllCaProductRelationship()
.push(this.setCaProductRelationshipModel(channel.type));
cartItem.caProductOffering.caCategory.getAllCategories()
.push(this.setCaCategoryModel("promo"));
cartItem.caProductOffering.caAttributes.getAllAttributes()
.push(this.setCaAttributesModel("Numero fijo Asociado", this.activeCustomerServiceProvider.getPhoneNumber()));
cartItem.cartItemRelationship.getAllCartItemRelationship()
.push(this.setCaCartItemRelationshipModel(channel.id, "reliesOn"));
});
});
});
}

Related

How filter and delete object with null or empty property in json query

I have this json :
{
"meta": {
"status": 200,
"pagination": {
"page": 1,
"perPage": 15,
"hasNext": true
}
},
"data": [
{
"id": "1",
"title": "Movie title1"
"rating": null,
"playProviders": [
]
},
{
"id": "2",
"title": "Movie title2"
"rating": {
"ratingAssessment": "7.1"
},
"playProviders": [
"HBO", "Netflix"
]
},
....
}
I want to create a page with a list of movies, I need to fetch movies but only those which have a rating and playProviders, what parameters should I use in this request?
https://api.com/movies?orderBy=views
When I filters in the code:
programs.filter((program) => program.rating !== null);
it only gets a few films per page, those that don't have null. For example, 15 are per page and I get 2. How do I filter this? (I am using react typescript)
I don't have access to the API code. I need to filter what is returned by the API or write a query so that you get already filtered data from the API.
programs = [
{rating: 1,
playProviders: ["sf"]
},
{
rating: 4,
playProviders: []
}
]
programs.filter(function(program) {
if (program.rating !== null && program.playProviders.length !== 0) {
return program;
}
})

React Native - Sort array based off words

Hey guys i have the following array:
Array [
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
I am going to take the users input in the form of a search bar, how can i sort the array based off the users input.
So lets say the user inputs
Bat g
the array would be sorted to:
Array [
Object {
"data": "Bat Girl",
"id": "2",
},
Object {
"data": "Cat Man",
"id": "1",
},
Object {
"data": "Mr Penguin",
"id": "3",
},
Object {
"data": "Cheeky Cheetah",
"id": "4",
},
]
How can I achieve this?
I have been searching around the array sort function:
Array.prototype.sort()
However I have only seen how to sort based off number comparisons I have never seen an array sorted based off string values like a search. Please could someone help me with this!
Here is function to search data using string text.
const searchItem = txt => {
let text = txt.toLowerCase();
let tracks = dataArray;
let filterTracks = tracks.filter(item => {
if (item.data.toLowerCase().match(text)) {
return item;
}
});
console.log('filterTracks', filterTracks);
};
Array Should be like this
var dataArray = [
{
data: 'Cat Man',
id: '1',
},
{
data: 'Bat Girl',
id: '2',
},
{
data: 'Mr Penguin',
id: '3',
},
{
data: 'Cheeky Cheetah',
id: '4',
},
];

How to transform an array into a Component in CSML?

I have created a chatbot in CSML and I'm trying to find a way to integrate an array into a carousel.
My problem is that I can't loop inside the Carousel component.
mainCourse:
// my array
do menu = [
{ "name": "Margarita", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/02-margherita.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Marinara", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/01-marinara.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Quatro Fromaggi", "type":"pizza", "image":"https://i2.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/08-quattro-formaggi.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Calzone", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/14-calzone-tav.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Vegetariana", "type":"pizza", "image":"https://i2.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/13-vegetariana.png?zoom=2&resize=150%2C150&ssl=1" }
...
]
say "Thank you, I also love {{firstChoice}} 😍"
say "Which pizza would you like? 🍕"
say Carousel(
cards = [
// Where I'd like to include my array
]
)
thanks!
You need to iterate through the Array in order to have a component-compatible list of Card() components, here is the doc for reference: https://docs.csml.dev/studio/channels/slack/message-formats#carousel-card
So the code below would work:
start:
do menu = [
{ "name": "Margarita", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/02-margherita.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Marinara", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/01-marinara.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Quatro Fromaggi", "type":"pizza", "image":"https://i2.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/08-quattro-formaggi.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Diavola", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/11-diavola.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Calzone", "type":"pizza", "image":"https://i1.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/14-calzone-tav.png?zoom=2&resize=150%2C150&ssl=1" },
{ "name": "Vegetariana", "type":"pizza", "image":"https://i2.wp.com/mycornerofitaly.com/wp-content/uploads/2016/11/13-vegetariana.png?zoom=2&resize=150%2C150&ssl=1" }
]
// You create an empty array
do cards = []
// You loop through your array and foreach entry you create a Card() component
// that you add to the array that'll you'll later use for the Carousel() component
foreach (v, i) in menu {
do newCard = Card(
title=v.name,
subtitle=v.type,
image_url=v.image,
buttons=[Button("Choose")]
)
do cards.push(newCard)
}
say Carousel(cards=cards)
goto end

Return one array of data in sub-document of Mongodb

I'm using Nodejs with Mongoose package.
Given I've something like this:-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat"
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse"
}
]
}
]
I want to get only the data of Jerry in pets array using it's _id (result shown below)
{
"_id": 2,
"name": "Jerry",
"category": "mouse"
}
Can I get it without needing to specify the _id of person 1 when using $elemMatch? Right now I code like this:-
const pet = People.find(
{ "_id": "1"}, // specifying 'person 1 _id' first
{ pets: { $elemMatch: { _id: 2 } } } // using 'elemMatch' to get 'pet' with '_id' of '2'
)
And it gave me what I want like I've shown you above. But is there any other way I can do this without needing to specify the _id of it's parent first (in this case, the _id of the people array)
Assuming nested array's _id's are unique you can filter by nested array elements directly:
const pet = People.find(
{ "pets._id": 2 },
{ pets: { $elemMatch: { _id: 2 } } }
)

Angular2 filter object array based on value inside property(nested filters)

I need to retrieve only the object that have the name ... as a participant using a filter(pipe).
Every object has an array of participants objects. This is what it looks like(see my example Json at the end)
-object
-- _id
-- participants
-- participant1
-- participant2
So this is what I tried:(hardcoded Jack to get a match...)
import { Pipe, PipeTransform } from "#angular/core";
#Pipe({ name: 'filter' })
export class Gamefilter implements PipeTransform {
public transform(values: any[], filter: string): string {
if (!values || !values.length) "";
if (!filter) return JSON.parse(JSON.stringify(values));
return JSON.parse(JSON.stringify(
values.filter((element) => {
return element.participants.filter((item)=> {
return item.name.indexOf("Jack") > 1;
});
})
));
}
}
Altough this doesn't work, and I can't see the mistake I made.
I tried this to (from Filtering array of objects with arrays based on nested value ):
values.filter((element) =>
element.participants.some((subElement) => subElement.name === "Jack"))
.map(element => {
let newElt = Object.assign({}, element); // copies element
return newElt.participants.filter(subElement => subElement.name === "Jack");
})
I also saw this post but could get the right answer for me: Filtering array based on value in deeply nested object in javascript
The example JSON:
[
{
"_id": "5925ae95675e19001106e940",
"createdOn": "2017-05-24T16:02:29.229Z",
"participants": [
{
"_id": "jack19302",
"name": "Jack",
"__v": 0
},
{
"_id": "donald38902",
"name": "Donald",
"__v": 0
}
]
},
{
"_id": "5925ae95675e19001106e990",
"createdOn": "2017-05-24T16:02:29.229Z",
"participants": [
{
"_id": "donald38902",
"name": "Donald",
"__v": 0
}
]
},
{
"_id": "5925ae95675e19001106e996",
"createdOn": "2017-05-24T16:02:29.229Z",
"participants": [
{
"_id": "jack19302",
"name": "Jack",
"__v": 0
}
]
}
]

Resources