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()
I have a json array like below and I use lodash for filtering
const data = [
{
"id": "66b2bc0b-2486-4bb0-a93c-0337ebe1d647",
"company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
"name": null,
"type": null,
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921WF-001E5E02C002",
"location_id": null,
"status": null,
"updated_at": "2020-11-17T07:05:57.037Z",
"created_at": "2020-11-17T07:05:57.037Z",
"created_by": null,
"updated_by": null
},
{
"id": "975c51f8-a6cb-4701-aaa6-077f8a9974a5",
"company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
"location_id": null,
"type": null,
"name": null,
"model": null,
"status": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921WF-001E5E02C001",
"created_by": null,
"updated_by": null,
"created_at": "2020-11-17T07:05:57.015Z",
"updated_at": "2020-11-17T07:05:57.167Z"
},
{
"id": "c4d5e446-c137-443c-a4d7-2c54c204c018",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": null,
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921TH-001E5E02C001_Naveen",
"location_id": null,
"status": "active",
"updated_at": "2020-11-18T12:10:56.027Z",
"created_at": "2020-11-18T12:10:56.027Z",
"created_by": null,
"updated_by": null
},
{
"id": "9287ce48-e60b-4e8a-96a5-cbedd1ee3be2",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": "smart-plug",
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921TH-001E5E02C002",
"location_id": null,
"status": null,
"updated_at": "2020-11-18T12:11:21.867Z",
"created_at": "2020-11-18T12:11:21.867Z",
"created_by": null,
"updated_by": null
},
{
"id": "6bf58e90-6a84-480e-b944-2cabb90f60c9",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": "gateway",
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921GW-001E5E02C002",
"location_id": null,
"status": "active",
"updated_at": "2020-11-18T12:12:23.431Z",
"created_at": "2020-11-18T12:12:23.431Z",
"created_by": null,
"updated_by": null
}
]
I need to filter the array based on multiple conditions for eg
filter from data where { status: "active","type": "gateway"};
It should return all the objects satisfying this condition. The where condition should be dynamic and should accept any supported field for filtering. I tried the below but unable to add multiple conditions
const filteredData = lodash.find(data, ['status', 'active']);
Is there a way to do it if so please help me with a sample code
For filtering the data you can use filter method of lodash library. I have attached a code snippet. Hope this helps to solve the problem.
const data = [{
"id": "66b2bc0b-2486-4bb0-a93c-0337ebe1d647",
"company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
"name": null,
"type": null,
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921WF-001E5E02C002",
"location_id": null,
"status": null,
"updated_at": "2020-11-17T07:05:57.037Z",
"created_at": "2020-11-17T07:05:57.037Z",
"created_by": null,
"updated_by": null
},
{
"id": "975c51f8-a6cb-4701-aaa6-077f8a9974a5",
"company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
"location_id": null,
"type": null,
"name": null,
"model": null,
"status": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921WF-001E5E02C001",
"created_by": null,
"updated_by": null,
"created_at": "2020-11-17T07:05:57.015Z",
"updated_at": "2020-11-17T07:05:57.167Z"
},
{
"id": "c4d5e446-c137-443c-a4d7-2c54c204c018",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": null,
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921TH-001E5E02C001_Naveen",
"location_id": null,
"status": "active",
"updated_at": "2020-11-18T12:10:56.027Z",
"created_at": "2020-11-18T12:10:56.027Z",
"created_by": null,
"updated_by": null
},
{
"id": "9287ce48-e60b-4e8a-96a5-cbedd1ee3be2",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": "smart-plug",
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921TH-001E5E02C002",
"location_id": null,
"status": null,
"updated_at": "2020-11-18T12:11:21.867Z",
"created_at": "2020-11-18T12:11:21.867Z",
"created_by": null,
"updated_by": null
},
{
"id": "6bf58e90-6a84-480e-b944-2cabb90f60c9",
"company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
"name": null,
"type": "gateway",
"model": null,
"serial_number": null,
"mac_address": null,
"firmware_verison": null,
"gateway_id": null,
"device_code": "ST921GW-001E5E02C002",
"location_id": null,
"status": "active",
"updated_at": "2020-11-18T12:12:23.431Z",
"created_at": "2020-11-18T12:12:23.431Z",
"created_by": null,
"updated_by": null
}
];
const filteredData = _.filter(data, {
status: "active",
type: "gateway"
})
console.log(filteredData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
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 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;
}, []);
});