I would like to take some of my nested object array and push to a new JSON Array object.
I would like for each channel to take publishedTracks to a new json obj and merge them all together to a new array obj.
My JSON:
[
{
"id": 1,
"user_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "channela1",
"slug": "",
"description": null,
"is_verified": 0,
"cover": null,
"avatar": null,
"created_at": null,
"updated_at": null,
"publishedTracks": [
{
"id": 1,
"album_id": 1,
"name": "track1",
"slug": "track1",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar1.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 1,
"channel_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "alb1",
"slug": "alb1",
"description": "labore qui non et et ratione nobis",
"release_date": null,
"avatar": "alb_avatar1.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 1,
"_pivot_channel_id": 1
},
{
"id": 27,
"album_id": 1,
"name": "track27",
"slug": "track27",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar27.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 1,
"channel_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "alb1",
"slug": "alb1",
"description": "qui non et et ratione sint officia nobis",
"release_date": null,
"avatar": "alb_avatar1.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 1,
"_pivot_channel_id": 1
},
{
"id": 2,
"album_id": 14,
"name": "track2",
"slug": "track2",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar2.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 14,
"channel_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "alb14",
"slug": "alb14",
"description": "aliquam odio sapiente architecto",
"release_date": null,
"avatar": "alb_avatar14.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 14,
"_pivot_channel_id": 1
},
{
"id": 28,
"album_id": 14,
"name": "track28",
"slug": "track28",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar28.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 14,
"channel_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "alb14",
"slug": "alb14",
"description": "aliquam odio sapiente architecto",
"release_date": null,
"avatar": "alb_avatar14.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 14,
"_pivot_channel_id": 1
}
]
},
{
"id": 2,
"user_id": 1,
"genre_id": null,
"genre_id_opt": null,
"name": "chann1",
"slug": "chann1",
"description": "adipisci non impedit tempora mollitia et est",
"is_verified": 0,
"cover": "chann_cover1.jpg",
"avatar": "chann_avatar1.jpg",
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"publishedTracks": [
{
"id": 3,
"album_id": 2,
"name": "track3",
"slug": "track3",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar3.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 2,
"channel_id": 2,
"genre_id": null,
"genre_id_opt": null,
"name": "alb2",
"slug": "alb2",
"description": "aliquid neque autem est dignissimos",
"release_date": null,
"avatar": "alb_avatar2.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 2,
"_pivot_channel_id": 2
},
{
"id": 29,
"album_id": 2,
"name": "track29",
"slug": "track29",
"track_no": null,
"track": null,
"duration": 0,
"avatar": "track_avatar29.jpg",
"genre_id": null,
"genre_id_opt": null,
"is_explicit": null,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"album": {
"id": 2,
"channel_id": 2,
"genre_id": null,
"genre_id_opt": null,
"name": "alb2",
"slug": "alb2",
"description": "aliquid neque autem est dignissimos",
"release_date": null,
"avatar": "alb_avatar2.jpg",
"type": null,
"is_publish": 1,
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z"
},
"_pivot_id": 2,
"_pivot_channel_id": 2
}
]
},
{
"id": 3,
"user_id": 6,
"genre_id": null,
"genre_id_opt": null,
"name": "chann2",
"slug": "chann2",
"description": "debitis repudiandae oluptatem quod",
"is_verified": 0,
"cover": "chann_cover2.jpg",
"avatar": "chann_avatar2.jpg",
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"publishedTracks": []
},
{
"id": 4,
"user_id": 3,
"genre_id": null,
"genre_id_opt": null,
"name": "chann3",
"slug": "chann3",
"description": "rerum ut sequuntur dolores",
"is_verified": 0,
"cover": "chann_cover3.jpg",
"avatar": "chann_avatar3.jpg",
"created_at": "2015-03-06T14:32:18.000Z",
"updated_at": "2015-03-06T14:32:18.000Z",
"publishedTracks": []
}
]
Are you looking for something like this ?
//channels is your array that you have put
var obj = [];
channels.forEach(function(channel) {
// it takes publishedTracks of the channel and pushes into a separate array.
obj.push(channel.publishedTracks);
//remove from channel object
delete channel.publishedTracks;
});
console.log(obj);
perhaps you might try
var newArray = JSONObject.map(function (elem) {
return elem.publishedTracks.reduce(function(prev, current) {
var result = prev;
result.push(current);
return result;
}, []);
});
Related
THIS IS MY CODE QUERY
i want to get only some specific field from every table:
const project = await Project.query()
.where("user_id", user_id)
.where('id', project_id)
.with('items.file.sheets.markup.comment.attachment').first()
but i get this response
there is lot of extra fields
"status": true,
"message": "Here is detail of Your Project",
"data": {
"id": 1,
"name": "Spacey",
"description": "222 please change some points",
"created_at": "2021-03-24 17:18:56",
"updated_at": "2021-03-24 19:12:09",
"deleted_at": null,
"items": [
{
"id": 1,
"project_id": 1,
"assignee_id": 1,
"status": null,
"type": "markup",
"description": "my ",
"version": 4,
"visibility": "1",
"due_date": "2019-12-31",
"priority": "high",
"resolved_on": null,
"created_by": 2,
"resolved_by": null,
"updated_by": null,
"created_at": "2021-03-24 17:19:02",
"updated_at": "2021-03-24 17:19:02",
"deleted_at": null,
"file": [
{
"id": 1,
"actual_name": "10Feb2021.pdf",
"original_ext": "pdf",
"random_name": "1616588342707_8063",
"original_local_path": "files/2/1/1/1616588342707_8063.pdf",
"image_url": "https://resolve-dev-backend.s3.us-east- "
"media_type": "profile",
"item_id": 1,
"user_id": 2,
"created_at": "2021-03-24 17:19:05",
"updated_at": "2021-03-24 17:19:05",
"created_by": 2,
"updated_by": null,
"deleted_at": null,
"sheets": [
{
"id": 1,
"actual_name": "convertpdftojpg.png",
"original_ext": "png",
"random_name": "1616588346705_117",
"original_local_path": "sheets/2/1/1/1616588346705_117.png",
"image_url": "https://resolve-dev-backend.s3.us-east-"
"type": "Sheet",
"item_id": 1,
"file_id": 1,
"user_id": 2,
"created_at": "2021-03-24 17:19:09",
"updated_at": "2021-03-24 17:19:09",
"created_by": 2,
"updated_by": null,
"deleted_at": null,
"markup": [
{
"id": 1,
"item_id": 1,
"sheet_id": 1,
"assignee_id": null,
"editor_details": "{\"a\": \"b\", \"c\": \"d\"}",
"visibility": "private",
"image_url": "{\"a\": \"b\", \"c\": \"d\"}",
"priority": "low",
"resolved_by": 2,
"resolved_on": "2021-03-",
"due_date": null,
"created_by": 2,
"created_at": "2021-03-24 18:30:21",
"updated_at": "2021-03-24 19:12:39",
"deleted_at": null,
"comment": []
}
]
}
]
}
],
but i don't need project_id etc like some field in every table
for
example i need only these field from file table
and want to get specific field from every table in my query
but i get above output response in my postman
```
"id": 1,
"actual_name": "10 -Rehan Shakeel - Feb2021.pdf",
"image_url": "https://resolve-dev/file/2/1/1/1616588346705_117.pdf",
"media_type": "profile",
"created_at": "2021-03-24 17:19:05",
"updated_at": "2021-03-24 17:19:05",
"deleted_at": null,
```
Try to add the select statement with the columns inside an array
const project = await Project.query()
.select(['tableyouwant.id', 'actual_name', 'image_url', 'media_type', ..........])
.where('id', project_id)
.with('items.file.sheets.markup.comment.attachment').first()
Let's say I have this object
[{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}]
I would get another object:
[{
"value": 2,
"text": "admin#example.com",
}, {
"value": 3,
"text": "agency_owner#example.com",
}, {
"value": 4,
"text": "license_owner#example.com",
}]
So, renaming "id" with "value" and "email" with "text".
I would use map and maybe not installing lodash or similar...
Thank you...
Well, map is a great idea. What is stopping you from using it?
You can:
obj = [{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}]
obj = obj.map(e => { return {
value: e.id,
text: e.email
}})
console.log(obj)
You can use map as you mentioned:
var arr = [{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}];
var result = arr.map(o => {
return { value: o.id, email: o.email}
});
console.log(result);
[{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}].map(function(obj){
return {
value:obj.id,
text:obj.email
}
})
I have in variable bookUnitIdInformacoes this array of objects:
[
{
"id": 5,
"book_id": 33,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 6,
"book_id": 33,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 7,
"book_id": 33,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
},
{
"id": 8,
"book_id": 39,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
I have in the variable idioma this array of objects:
[
{
"id": 13,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 10:04:47",
"updated_at": "2020-01-05 16:08:00",
"language_substring": "US"
},
{
"id": 14,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:06:44",
"updated_at": "2019-12-30 11:06:44",
"language_substring": null
}
]
I need to create a for() that while the column book_id is equal the index+1, insert in idioma[i].quiz the value of the bookUnitIdInformacoes[i] and when the book_id of the bookUnitIdInformacoes array is different, put in the next position of idioma[i]quiz, so i need this json:
[
{
"id": 13,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 10:04:47",
"updated_at": "2020-01-05 16:08:00",
"language_substring": "US",
"quiz": [
{
"id": 5,
"book_id": 33,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 6,
"book_id": 33,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 7,
"book_id": 33,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
}
]
},
{
"id": 14,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:06:44",
"updated_at": "2019-12-30 11:06:44",
"language_substring": null,
"quiz": [
{
"id": 8,
"book_id": 39,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
}
]
I try something like:
for(let i=0;i<quizAbertos.length;i++){
if(i+1 === quizAbertos.length){
break;
}else{
if(bookUnitIdInformacoes[i].book_id === bookUnitIdInformacoes[i+1].book_id){
idioma[i].quiz = bookUnitIdInformacoes[i]
}
}
But i'm getting wrong json..
#Edit:
Actually i'm trying something like:
let book_id
let i_book_id = 0
let i_mudou_book_id = 0;
for(let i=0;i<bookUnitIdInformacoes.length;i++){
if(bookUnitIdInformacoes[i+1] === undefined){
book_id = bookUnitIdInformacoes[bookUnitIdInformacoes.length-1].book_id
}else{
if(bookUnitIdInformacoes[i].book_id === bookUnitIdInformacoes[i+1].book_id){
i_mudou_book_id++
}
}
}
idioma[0].quiz = bookUnitIdInformacoes.splice(0,i_mudou_book_id+1)
idioma[1].quiz = bookUnitIdInformacoes
but this way if i have more than 2 length i will be have problems and if i have only one length i will be too have problems.
First, you can group the bookUnitIdInfomacoes by book_id. This can be done by reducing the array into an object of key => value pairs as book_id => array of books:
{
"33": [
{
"id": 5,
"book_id": 33,
...
},
{
"id": 6,
"book_id": 33,
...
},
{
"id": 7,
"book_id": 33,
...
}
],
"39": [
{
"id": 8,
"book_id": 39,
...
}
]
}
Then, using Object.values will allow us to retrieve only the values of this groupedObj object. This will give us:
[
[
{
"id": 5,
"book_id": 33,
...
},
{
"id": 6,
"book_id": 33,
...
},
{
"id": 7,
"book_id": 33,
...
}
],
[
{
"id": 8,
"book_id": 39,
...
}
]
]
Lastly, we will have to map idiomas and add the corresponding book group into a new property called quizz. For a given idioma, we know its position in idiomas thanks to the second argument of map: i. We can simply do grouped[i] to get the corresponding group of books.
const groupedObj = bookUnitIdInformacoes.reduce((grouped, info) => {
grouped[info.book_id] = grouped[info.book_id] || [];
grouped[info.book_id].push(info);
return grouped;
}, {});
const grouped = Object.values(groupedObj);
const result = idiomas.map((idioma, i) => ({
...idioma,
quizz: grouped[i]
}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script>const bookUnitIdInformacoes=[{id:5,book_id:33,unit:1,sequence:1,description:"UNIT_01_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:21",updated_at:"2019-12-30 14:54:12",miniature:null},{id:6,book_id:33,unit:1,sequence:2,description:"UNIT_01_VOCABULARY",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:39",updated_at:"2019-12-27 08:11:39",miniature:null},{id:7,book_id:33,unit:2,sequence:1,description:"UNIT_02_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:46",updated_at:"2019-12-27 08:11:46",miniature:null},{id:8,book_id:39,unit:1,sequence:1,description:"UNIT_01_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-30 11:07:09",updated_at:"2019-12-30 15:03:50",miniature:null}],idiomas=[{id:13,code:"ING-NOT-2019",description:"Inglês Noturno 2019",start_date:"2019-12-30T03:00:00.000Z",end_date:"2019-12-31T03:00:00.000Z",period:"Noturno",language:"Inglês",status:!1,user_id:1,created_at:"2019-12-30 10:04:47",updated_at:"2020-01-05 16:08:00",language_substring:"US"},{id:14,code:"ESP-MAN-2019",description:"Espanhol manhã 2019",start_date:"2019-12-30T03:00:00.000Z",end_date:"2019-12-31T03:00:00.000Z",period:"Manhã",language:"Espanhol",status:!1,user_id:1,created_at:"2019-12-30 11:06:44",updated_at:"2019-12-30 11:06:44",language_substring:null}];</script>
I like to know how many items have the value material_delivery is 1?
I like to do it in a vue method.
[{
"id": 43,
"uuid": "c92421d0-71cc-433d-b7b5-fc6c91c2a3a4",
"project_id": 8,
"name": "Konstruktionsfreigabe",
"due_date": "2019-10-18",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:09:41"
}, {
"id": 44,
"uuid": "a063964b-f8fc-4c28-9055-09ed5fc4b8dd",
"project_id": 8,
"name": "Material",
"due_date": "2019-12-13",
"material_delivery": 1,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:06:37"
}, {
"id": 45,
"uuid": "7de3410f-82c2-4b30-8e69-56906b16da4b",
"project_id": 8,
"name": "Montageende",
"due_date": "2019-12-16",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}, {
"id": 46,
"uuid": "8b034697-543c-46f6-a5be-104011700fb9",
"project_id": 8,
"name": "Lieferung",
"due_date": "2020-01-25",
"material_delivery": 1,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:57:16"
}, {
"id": 47,
"uuid": "ec1101cf-97cc-4eed-a2c6-0685b7cc073b",
"project_id": 8,
"name": "Abnahme",
"due_date": "2020-03-05",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}, {
"id": 48,
"uuid": "deb7324a-64f2-4e1c-a358-87fdb95430ea",
"project_id": 8,
"name": "Rechnung",
"due_date": "2020-04-14",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}]
I tried with filter but no success.
Finally, I like to know, if there are more than one item with the value 1 in material_delivery
There are many ways to do this. You could use filter, like you initially tried:
methods: {
countMaterialDelivery(array) {
return array.filter(item => item.material_delivery === 1).length;
}
}
Javascript newbie here :) I want to achieve sth but can't figure it out.
Here is my data .
<pre>
[
[
{
"id": 2759178563,
"title": "Ergonomic Paper Computer",
"handle": "ergonomic-paper-computer",
"body_html": "Enable turn-key infrastructures",
"published_at": "2015-09-23T20:51:49-04:00",
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"vendor": "Schinner Inc",
"product_type": "Computer",
"tags": [
"Computer",
"Ergonomic",
"Paper"
],
"variants": [
{
"id": 8041863299,
"title": "White",
"option1": "White",
"option2": null,
"option3": null,
"price": "67.25",
"grams": 5145,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863363,
"title": "Mint green",
"option1": "Mint green",
"option2": null,
"option3": null,
"price": "29.58",
"grams": 6860,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863491,
"title": "Yellow",
"option1": "Yellow",
"option2": null,
"option3": null,
"price": "54.19",
"grams": 6045,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041863555,
"title": "Blue",
"option1": "Blue",
"option2": null,
"option3": null,
"price": "22.88",
"grams": 9526,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759178563,
"created_at": "2015-09-23T20:51:49-04:00",
"updated_at": "2015-09-23T20:51:49-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642046019,
"created_at": "2015-09-23T20:51:49-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:49-04:00",
"product_id": 2759178563,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Ergonomic_20Paper_20Computer.png?v=1443055909"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"White",
"Mint green",
"Yellow",
"Blue"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
],
[
{
"id": 2759168323,
"title": "Awesome Cotton Computer",
"handle": "awesome-cotton-computer-1",
"body_html": "Brand synergistic applications",
"published_at": "2015-09-23T20:51:24-04:00",
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"vendor": "Hills Group",
"product_type": "Computer",
"tags": [
"Awesome",
"Computer",
"Cotton"
],
"variants": [
{
"id": 8041841795,
"title": "Black",
"option1": "Black",
"option2": null,
"option3": null,
"price": "2.05",
"grams": 1906,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841859,
"title": "Orchid",
"option1": "Orchid",
"option2": null,
"option3": null,
"price": "10.78",
"grams": 4970,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841923,
"title": "Tan",
"option1": "Tan",
"option2": null,
"option3": null,
"price": "50.54",
"grams": 6738,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041841987,
"title": "Teal",
"option1": "Teal",
"option2": null,
"option3": null,
"price": "91.51",
"grams": 8718,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041842051,
"title": "Gold",
"option1": "Gold",
"option2": null,
"option3": null,
"price": "8.24",
"grams": 194,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 5,
"product_id": 2759168323,
"created_at": "2015-09-23T20:51:24-04:00",
"updated_at": "2015-09-23T20:51:24-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642032131,
"created_at": "2015-09-23T20:51:24-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:24-04:00",
"product_id": 2759168323,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Cotton_20Computer_f74fc2a4-efa5-42ca-a3b2-36c378f1c003.png?v=1443055884"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Black",
"Orchid",
"Tan",
"Teal",
"Gold"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
],
[
{
"id": 2759167747,
"title": "Awesome Bronze Computer",
"handle": "awesome-bronze-computer",
"body_html": "Orchestrate holistic web services",
"published_at": "2015-09-23T20:51:20-04:00",
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"vendor": "Zboncak-Kemmer",
"product_type": "Computer",
"tags": [
"Awesome",
"Bronze",
"Computer"
],
"variants": [
{
"id": 8041840195,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "56.69",
"grams": 6035,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840259,
"title": "Mint green",
"option1": "Mint green",
"option2": null,
"option3": null,
"price": "63.89",
"grams": 9961,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 2,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840323,
"title": "Maroon",
"option1": "Maroon",
"option2": null,
"option3": null,
"price": "18.76",
"grams": 922,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 3,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
},
{
"id": 8041840387,
"title": "Salmon",
"option1": "Salmon",
"option2": null,
"option3": null,
"price": "97.60",
"grams": 1355,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 4,
"product_id": 2759167747,
"created_at": "2015-09-23T20:51:20-04:00",
"updated_at": "2015-09-23T20:51:20-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642031235,
"created_at": "2015-09-23T20:51:20-04:00",
"position": 1,
"updated_at": "2015-09-23T20:51:20-04:00",
"product_id": 2759167747,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Bronze_20Computer.png?v=1443055880"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta",
"Mint green",
"Maroon",
"Salmon"
]
}
]
},
{
"id": 2759192387,
"title": "Heavy Duty Concrete Keyboard",
"handle": "heavy-duty-concrete-keyboard",
"body_html": "Strategize synergistic e-markets",
"published_at": "2015-09-23T20:52:07-04:00",
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"vendor": "Stiedemann and Sons",
"product_type": "Keyboard",
"tags": [
"Concrete",
"Duty",
"Heavy",
"Keyboard"
],
"variants": [
{
"id": 8041883779,
"title": "Magenta",
"option1": "Magenta",
"option2": null,
"option3": null,
"price": "14.20",
"grams": 3030,
"compare_at_price": null,
"sku": "",
"requires_shipping": true,
"taxable": true,
"position": 1,
"product_id": 2759192387,
"created_at": "2015-09-23T20:52:07-04:00",
"updated_at": "2015-09-23T20:52:07-04:00",
"available": true,
"featured_image": null
}
],
"images": [
{
"id": 5642056899,
"created_at": "2015-09-23T20:52:07-04:00",
"position": 1,
"updated_at": "2015-09-23T20:52:07-04:00",
"product_id": 2759192387,
"variant_ids": [],
"src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927"
}
],
"options": [
{
"name": "Title",
"position": 1,
"values": [
"Magenta"
]
}
]
}
]
]
</pre>
And here is my code:
import { Injectable } from '#angular/core';
import { Http, Response } from '#angular/http';
#Injectable()
export class ShopifyService {
/**
* Initialization
*/
constructor (
private http: Http
) {}
private shopifyUrl = 'app/heroes.json';
errorMessage: string;
/**
* Private Methods
*/
fetchProducts(): Promise<Response> {
return this.http.get(this.shopifyUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
let body = res.json();
if (body.data) {
return body.data;
}
else if (body.products) {
return body.products;
}
else {
return { };
}
}
private handleError (error: any) {
let errMsg = error.message || 'Server error';
console.error(errMsg);
return Promise.reject(errMsg);
}
private filterProducts(list, archetype) {
var result = [];
for (var i = 0; i < list.length; i++) {
if (list[i].product_type === archetype) {
result.push(list[i]);
}
}
return result;
}
/**
* Public Methods
*/
public findProducts(archetypes) {
return this.fetchProducts().then(
products => {
var result = [],
fetchedResponse = [];
for (var i = 0; i < archetypes.length; i++) {
fetchedResponse = this.filterProducts(
products, archetypes[i]
);
result = result.concat(fetchedResponse);
}
return result;
},
error => this.errorMessage = <any>error
)
}
public findSingleProductVariant() {
var result = [],
j = 0;
return this.findVariants().then(
product => {
for (var i = 0; i < product.length; i++) {
// console.log(product[i]);
}
}
);
// return result;
}
findVariants() {
return this.findProducts(['Computer', 'Keyboard']).then(
products => {
var result = [];
for (var i = 0; i < products.length; i++) {
if (products[i].product_type === 'Computer') {
for (var j = 0; j < products.length; j++) {
if (products[j].product_type === 'Keyboard') {
result.push([products[i], products[j]]);
}
}
}
}
console.log(result);
return result;
}
)
}
}
Until here I got to half of what. An array of two dimensional arrays (one of them is from keyboard type and another one from computer type).
It is sth like this ==>
[
[
[Computer_A],
[Keyboard_A]
],
[
[Computer_B],
[Keyboard_A]
],
[
[Computer_C],
[Keyboard_A]
],
]
Now each one of these objects have its own array of variants. I have to modify my findSingleProductVariant in order to go over each one of these arrays, pick a keyboard variant (which its array has a price field), also pick a computer variant and calculate the price. If the price is less than 1000, it can continue picking another keyboard and computer. But the thing is that it can't have two identical arrays. for instance we can't have sth like this.
[
['Computer_A'], ['Keyboard_A'],
['Computer_A'], ['Keyboard_A'],
]
The key thing here is that you can't directly compare objects and expect relevant results, e.g.
['Red Lamp', 'Blue Table'] === ['Red Lamp', 'Blue Table']
will return false (Array.indexOf() also won't work as expected since it uses strict comparison).
So for an array of arrays, you will have to use two nested loops to get down to comparing primitive values.