Related
I am trying to filter an array and only show items if the date matches the value I assign. I have only ever filtered an array that has objects before, but this one has nested arrays and I'm struggling to figure it out.
const branches = [{
"id": "0741",
"name": "Manchester Hospital",
"appointments": [{
"id": "2000",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:55",
"14:10"
]
},
{
"date": "2021-09-13",
"startTimes": [
"10:40",
"11:30",
"11:55",
"14:35",
"15:00",
"15:45",
"16:05"
]
}
]
}]
},
{
"id": "0327",
"name": "London Hospital",
"appointments": [{
"id": "2000",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:45",
"14:05",
"14:25",
"14:45",
"15:05",
"16:25"
]
},
{
"date": "2021-09-10",
"startTimes": [
"13:25",
"13:45",
"13:50",
"14:05",
"14:10",
"14:25",
"14:30",
"14:45",
"14:50",
"15:30",
"15:45"
]
}
]
}]
},
{
"id": "0221",
"name": "Newcastle Hospital",
"appointments": [{
"id": "2000",
"name": "ST",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"08:00"
]
},
{
"date": "2021-09-10",
"startTimes": [
"09:00"
]
},
{
"date": "2021-09-13",
"startTimes": [
"08:35",
"08:55",
"09:00",
"14:55"
]
}
]
}]
},
];
I am able to do something simple like this which brings back the object that contains this value
const tngCharacters = branches.filter(branch => {
return branch.name.includes('London Hospital');
});
console.log(tngCharacters);
But, I want to bring back all objects based on whether the date is present that I choose. So, ideally the below would bring back the full objects for Newcastle and Manchester Hospital
let chosenDate = "2021-09-13";
const tngCharacters = branches.filter(branch => {
return branch.appointments.dates.date.includes(chosenDate);
});
console.log(tngCharacters);
I think the issue is that appointments and dates are nested arrays and those values would need an index to check each, but I have no idea how to do this.
So, my expected result based on the chosen date would bring back all of the following information
branches = [{
"id": "0741",
"name": "Manchester Hospital",
"appointments": [{
"id": "2000",
"dates": [
{
"date": "2021-09-13",
"startTimes": [
"10:40",
"11:30",
"11:55",
"14:35",
"15:00",
"15:45",
"16:05"
]
}
]
}]
},
{
"id": "0221",
"name": "Newcastle Hospital",
"appointments": [{
"id": "2000",
"name": "ST",
"buildDate": "2021-10-22",
"dates": [
{
"date": "2021-09-13",
"startTimes": [
"08:35",
"08:55",
"09:00",
"14:55"
]
}
]
}]
},
];
Your issue, I think, is that you have to work with the arrays inside apppointments.
let chosenDate = "2021-09-13";
const tngCharacters = branches.filter(branch => {
for (let i=0; i < branch.appointments.length; i++){
for (let j=0; j < branch.appointments[i].dates.length; j++){
if (branch.appointments[i].dates[j].date === chosenDate) return true;
}
}
return false
});
Try that... might work.
EDIT actually with forEach it probably won't work. You might need to use nested for loops.
Because you have 3 levels of nesting, you have to iterate through each of them.
Your dates are an array inside an array, so you have to iterate through it to find nested elements too. I.e. :
let chosenDate = "2021-09-13";
const tngCharacters = branches.filter(branch => {
console.log(branch)
const inludedSubDates = branch.appointments.filter(a => {
const thirdLevel = a.dates.filter(e => e.date.includes(chosenDate))
// // now you have a subarray that is filtered by the desired search term
// // you can do any manipulation, i.e. check if the sub array is empty or any
// //logic you need
if (thirdLevel.length > 0)
return true;
} )
if (inludedSubDates.length > 0)
return true;
}) ;
console.log(tngCharacters);
Please try if below works for you. This is using forEach, please check the other example using filter. Check the optimized method using some at the end of the post
const branches = [{
"id": "0741",
"name": "Manchester Hospital",
"appointments": [{
"id": "2000",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:55",
"14:10"
]
},
{
"date": "2021-09-13",
"startTimes": [
"10:40",
"11:30",
"11:55",
"14:35",
"15:00",
"15:45",
"16:05"
]
}
]
}]
},
{
"id": "0327",
"name": "London Hospital",
"appointments": [{
"id": "2000",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:45",
"14:05",
"14:25",
"14:45",
"15:05",
"16:25"
]
},
{
"date": "2021-09-10",
"startTimes": [
"13:25",
"13:45",
"13:50",
"14:05",
"14:10",
"14:25",
"14:30",
"14:45",
"14:50",
"15:30",
"15:45"
]
}
]
}]
},
{
"id": "0221",
"name": "Newcastle Hospital",
"appointments": [{
"id": "2000",
"name": "ST",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"08:00"
]
},
{
"date": "2021-09-10",
"startTimes": [
"09:00"
]
},
{
"date": "2021-09-13",
"startTimes": [
"08:35",
"08:55",
"09:00",
"14:55"
]
}
]
}]
},
];
let chosenDate = "2021-09-13";
let chosenBranches = []; // to store the chosen branches
branches.forEach(branch => {
branch.appointments.forEach(({
dates
}) => {
dates.forEach(eachDate => {
if (chosenDate === eachDate.date) {
chosenBranches.push(branch);
}
});
});
});
console.log(chosenBranches);
Using filter
const branches = [{
"id": "0741",
"name": "Manchester Hospital",
"appointments": [{
"id": "2000",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:55",
"14:10"
]
},
{
"date": "2021-09-13",
"startTimes": [
"10:40",
"11:30",
"11:55",
"14:35",
"15:00",
"15:45",
"16:05"
]
}
]
}]
},
{
"id": "0327",
"name": "London Hospital",
"appointments": [{
"id": "2000",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:45",
"14:05",
"14:25",
"14:45",
"15:05",
"16:25"
]
},
{
"date": "2021-09-10",
"startTimes": [
"13:25",
"13:45",
"13:50",
"14:05",
"14:10",
"14:25",
"14:30",
"14:45",
"14:50",
"15:30",
"15:45"
]
}
]
}]
},
{
"id": "0221",
"name": "Newcastle Hospital",
"appointments": [{
"id": "2000",
"name": "ST",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"08:00"
]
},
{
"date": "2021-09-10",
"startTimes": [
"09:00"
]
},
{
"date": "2021-09-13",
"startTimes": [
"08:35",
"08:55",
"09:00",
"14:55"
]
}
]
}]
},
];
let chosenDate = "2021-09-13";
const chosenBranches = branches.filter(branch => {
return Boolean(branch.appointments.filter(({
dates
}) => {
return Boolean(dates.filter(eachDate => {
return chosenDate === eachDate.date
}).length);
}).length);
});
console.log(chosenBranches);
A more optimized way using Array.some, this is optimized because this will not loop the entire array, some will exit iterating the array when the first instance where chosen date is encountered.
const branches = [{
"id": "0741",
"name": "Manchester Hospital",
"appointments": [{
"id": "2000",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:55",
"14:10"
]
},
{
"date": "2021-09-13",
"startTimes": [
"10:40",
"11:30",
"11:55",
"14:35",
"15:00",
"15:45",
"16:05"
]
}
]
}]
},
{
"id": "0327",
"name": "London Hospital",
"appointments": [{
"id": "2000",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"12:45",
"14:05",
"14:25",
"14:45",
"15:05",
"16:25"
]
},
{
"date": "2021-09-10",
"startTimes": [
"13:25",
"13:45",
"13:50",
"14:05",
"14:10",
"14:25",
"14:30",
"14:45",
"14:50",
"15:30",
"15:45"
]
}
]
}]
},
{
"id": "0221",
"name": "Newcastle Hospital",
"appointments": [{
"id": "2000",
"name": "ST",
"buildDate": "2021-10-22",
"dates": [{
"date": "2021-09-09",
"startTimes": [
"08:00"
]
},
{
"date": "2021-09-10",
"startTimes": [
"09:00"
]
},
{
"date": "2021-09-13",
"startTimes": [
"08:35",
"08:55",
"09:00",
"14:55"
]
}
]
}]
},
];
let chosenDate = "2021-09-13";
const chosenBranches = branches.filter(branch => {
return branch.appointments.some(({
dates
}) => {
return dates.some(eachDate => {
return chosenDate === eachDate.date
});
});
});
console.log(chosenBranches);
Following code gets the result below in a way that multiple iterations required. I wonder what would be the way to make it happen in a single or less iterations. Thanks in advance.
var input = [{
"ActiveMembers": [{
"Id": 101,
"Name": "alpha"
}, {
"Id": 102,
"Name": "bravo"
}],
"Contents": [{
"Id": 2001,
"RowId": "517",
"Time": "19 Jan 2017",
"ViewCount": 1124
}, {
"Id": 2002,
"RowId": "518",
"Time": "Today, 07:02 PM",
"ViewCount": 62
}],
"TotalUsers": 3,
"UsersDetails": "2 members, 1 anonymous users"
}, {
"ActiveMembers": [{
"Id": 101,
"Name": "alpha"
}, {
"Id": 103,
"Name": "charlie"
}, {
"Id": 104,
"Name": "delta"
}, {
"Id": 105,
"Name": "bravo"
}],
"Contents": [{
"Id": 2002,
"RowId": "519",
"Time": "27 Jun 2017",
"ViewCount": 4833
}, {
"Id": 2041,
"RowId": "525",
"Time": "17 Feb 2015",
"ViewCount": 24491
}],
"TotalUsers": 23,
"UsersDetails": "4 members, 19 anonymous users"
}];
var contents = Array.prototype.concat.apply([], input.map(i => i.Contents));
var activeMembers = _.uniqBy(Array.prototype.concat.apply([], input.map(i => i.ActiveMembers)), (i) => i.Id);
var totalUsers = number = _.sumBy(input, (i) => i.TotalUsers);
var userDetails = string = input.map(i => i.UsersDetails).join(' ; ');
const result = new Object();
result.Contents = contents;
result.ActiveMembers = activeMembers;
result.TotalUsers = totalUsers;
result.UserDetails = userDetails;
console.log(JSON.stringify(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Result
{
"ActiveMembers": [
{
"Id": 101,
"Name": "alpha"
},
{
"Id": 102,
"Name": "bravo"
},
{
"Id": 103,
"Name": "charlie"
},
{
"Id": 104,
"Name": "delta"
},
{
"Id": 105,
"Name": "bravo"
}
],
"Contents": [
{
"Id": 2001,
"RowId": "517",
"Time": "19 Jan 2017",
"ViewCount": 1124
},
{
"Id": 2002,
"RowId": "518",
"Time": "Today, 07:02 PM",
"ViewCount": 62
},
{
"Id": 2002,
"RowId": "519",
"Time": "27 Jun 2017",
"ViewCount": 4833
},
{
"Id": 2041,
"RowId": "525",
"Time": "17 Feb 2015",
"ViewCount": 24491
}
],
"TotalUsers": 26,
"UsersDetails": "2 members, 1 anonymous users;4 members, 19 anonymous users"
}
Aggregate the data in a single iteration.
let ActiveMembers = [];
let Contents = [];
let TotalUsers = 0;
let UserDetails = [];
input.forEach((item) => {
ActiveMembers = ActiveMembers.concat(item.ActiveMembers);
Contents = Contents.concat(item.Contents);
TotalUsers += item.TotalUsers;
UserDetails.push(item.UsersDetails);
});
const result = {
ActiveMembers: _.uniqBy(ActiveMembers, "Id"),
Contents: Contents,
TotalUsers: TotalUsers,
UserDetails: UserDetails.join(";")
};
console.log(JSON.stringify(result));
Let's say I have an object like this:
var object = {
"Defender": {
"player-1868": {
"birthdate": "1 July 1996",
"club_country": "IQ",
"club_id": 171,
"club_name": "Erbil",
"forename": "Burhan Jumaah",
"id": 1868,
"league_id": 12,
"league_name": "Iraqi Premier League",
"name": "Burhan Jumaah",
"nationality": "iq",
"nationality_full": "Iraq",
"position": "Defender",
"surname": "Razzaq",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
},
"Goalkeeper": {
"player-3076": {
"birthdate": "15 December 1985",
"club_country": "QA",
"club_id": 1,
"club_name": "Lekhwiya",
"comments": [
{
"comment": "xxx",
"name": "guy tester",
"photoURL": "http://pbs.twimg.com/profile_images/855450315704979460/RGiq07D7_normal.jpg",
"time": 1496529030321,
"user": "y884F42mLCVdld5V5cMeRpl11gJ2"
}
],
"forename": "Qasem Abdulhamed",
"id": 3076,
"league_id": 1,
"league_name": "Qatar Stars League",
"name": "Qasem Burhan",
"nationality": "qa",
"nationality_full": "Qatar",
"position": "Goalkeeper",
"surname": "Burhan",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
},
"player-3532": {
"birthdate": "2 April 1992",
"club_country": "SA",
"club_id": 18,
"club_name": "Al Ittihad",
"forename": "Fawaz",
"id": 3532,
"league_id": 2,
"league_name": "Saudi Professional League",
"name": "Fawaz Al Qarni",
"nationality": "sa",
"nationality_full": "Saudi Arabia",
"position": "Goalkeeper",
"surname": "Al Qarni",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
}
};
How would I, using lodash, traverse through this object and add every time the property id appears inside an object key of player-xxxxxx, add that value to an array. Essentially, getting all of the player IDs in a single array?
You can Array#reduce recursively on the object's keys to find the search string (player-), and create and extract the values:
var object = {"Defender":{"player-1868":{"birthdate":"1 July 1996","club_country":"IQ","club_id":171,"club_name":"Erbil","forename":"Burhan Jumaah","id":1868,"league_id":12,"league_name":"Iraqi Premier League","name":"Burhan Jumaah","nationality":"iq","nationality_full":"Iraq","position":"Defender","surname":"Razzaq","votes":["y884F42mLCVdld5V5cMeRpl11gJ2"]}},"Goalkeeper":{"player-3076":{"birthdate":"15 December 1985","club_country":"QA","club_id":1,"club_name":"Lekhwiya","comments":[{"comment":"xxx","name":"guy tester","photoURL":"http://pbs.twimg.com/profile_images/855450315704979460/RGiq07D7_normal.jpg","time":1496529030321,"user":"y884F42mLCVdld5V5cMeRpl11gJ2"}],"forename":"Qasem Abdulhamed","id":3076,"league_id":1,"league_name":"Qatar Stars League","name":"Qasem Burhan","nationality":"qa","nationality_full":"Qatar","position":"Goalkeeper","surname":"Burhan","votes":["y884F42mLCVdld5V5cMeRpl11gJ2"]},"player-3532":{"birthdate":"2 April 1992","club_country":"SA","club_id":18,"club_name":"Al Ittihad","forename":"Fawaz","id":3532,"league_id":2,"league_name":"Saudi Professional League","name":"Fawaz Al Qarni","nationality":"sa","nationality_full":"Saudi Arabia","position":"Goalkeeper","surname":"Al Qarni","votes":["y884F42mLCVdld5V5cMeRpl11gJ2"]}}};
function searchProps(searchStr, object) {
return Object.keys(object).reduce(function(arr, key) { // reduce the objects keys to an array
key.indexOf(searchStr) === -1 || arr.push(key.slice(searchStr.length)); // if a key contains the search string, take whatever after it
var propValue = object[key];
if(typeof propValue === 'object' && propValue !== null) { // if the value of the property is an array, run it through search props
return arr.concat(searchProps(searchStr, propValue));
}
return arr;
}, []);
}
var result = searchProps('player-', object);
console.log(result);
With Javascript, to traverse the keys and get each id, you may use "for" like below sample code:
var obj = {
"Defender": {
"player-1868": {
"birthdate": "1 July 1996",
"club_country": "IQ",
"club_id": 171,
"club_name": "Erbil",
"forename": "Burhan Jumaah",
"id": 1868,
"league_id": 12,
"league_name": "Iraqi Premier League",
"name": "Burhan Jumaah",
"nationality": "iq",
"nationality_full": "Iraq",
"position": "Defender",
"surname": "Razzaq",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
},
"Goalkeeper": {
"player-3076": {
"birthdate": "15 December 1985",
"club_country": "QA",
"club_id": 1,
"club_name": "Lekhwiya",
"comments": [
{
"comment": "xxx",
"name": "guy tester",
"photoURL": "http://pbs.twimg.com/profile_images/855450315704979460/RGiq07D7_normal.jpg",
"time": 1496529030321,
"user": "y884F42mLCVdld5V5cMeRpl11gJ2"
}
],
"forename": "Qasem Abdulhamed",
"id": 3076,
"league_id": 1,
"league_name": "Qatar Stars League",
"name": "Qasem Burhan",
"nationality": "qa",
"nationality_full": "Qatar",
"position": "Goalkeeper",
"surname": "Burhan",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
},
"player-3532": {
"birthdate": "2 April 1992",
"club_country": "SA",
"club_id": 18,
"club_name": "Al Ittihad",
"forename": "Fawaz",
"id": 3532,
"league_id": 2,
"league_name": "Saudi Professional League",
"name": "Fawaz Al Qarni",
"nationality": "sa",
"nationality_full": "Saudi Arabia",
"position": "Goalkeeper",
"surname": "Al Qarni",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
}
};
var arr = [];
tgtObj = obj["Defender"];
for(var key in tgtObj){
if (tgtObj.hasOwnProperty(key)){
console.log(key);
arr.push(tgtObj[key]["id"]);
}
}
tgtObj = obj["Goalkeeper"];
for(var key in tgtObj){
if (tgtObj.hasOwnProperty(key)){
console.log(key);
arr.push(tgtObj[key]["id"]);
}
}
console.log(arr);
Iterate through the object keys check to see if the string starts with "player-" and then push into an empty array the last four characters.
var object = {
"Defender": {
"player-1868": {
"birthdate": "1 July 1996",
"club_country": "IQ",
"club_id": 171,
"club_name": "Erbil",
"forename": "Burhan Jumaah",
"id": 1868,
"league_id": 12,
"league_name": "Iraqi Premier League",
"name": "Burhan Jumaah",
"nationality": "iq",
"nationality_full": "Iraq",
"position": "Defender",
"surname": "Razzaq",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
},
"Goalkeeper": {
"player-3076": {
"birthdate": "15 December 1985",
"club_country": "QA",
"club_id": 1,
"club_name": "Lekhwiya",
"comments": [{
"comment": "xxx",
"name": "guy tester",
"photoURL": "http://pbs.twimg.com/profile_images/855450315704979460/RGiq07D7_normal.jpg",
"time": 1496529030321,
"user": "y884F42mLCVdld5V5cMeRpl11gJ2"
}],
"forename": "Qasem Abdulhamed",
"id": 3076,
"league_id": 1,
"league_name": "Qatar Stars League",
"name": "Qasem Burhan",
"nationality": "qa",
"nationality_full": "Qatar",
"position": "Goalkeeper",
"surname": "Burhan",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
},
"player-3532": {
"birthdate": "2 April 1992",
"club_country": "SA",
"club_id": 18,
"club_name": "Al Ittihad",
"forename": "Fawaz",
"id": 3532,
"league_id": 2,
"league_name": "Saudi Professional League",
"name": "Fawaz Al Qarni",
"nationality": "sa",
"nationality_full": "Saudi Arabia",
"position": "Goalkeeper",
"surname": "Al Qarni",
"votes": [
"y884F42mLCVdld5V5cMeRpl11gJ2"
]
}
}
};
function getPlayers(object) {
let items = [];
for (let i in object) {
let key = Object.keys(object[i]);
key.forEach(i => i.startsWith('player') ? items.push(i.substring(i.length -4)) : '');
}
return items;
}
console.log(getPlayers(object));
I was able to solve it myself after some more time on it.
var obj = {
Defender: {
"player-1868": {
club_id: 171,
id: 1868,
league_id: 12,
name: "Burhan Jumaah"
}
},
Goalkeeper: {
"player-3076": {
club_id: 1,
id: 3076,
league_id: 1,
name: "Qasem Burhan"
},
"player-3532": {
club_id: 18,
id: 3532,
league_id: 2,
name: "Fawaz Al Qarni"
}
}
};
function searchForProp(lookingFor, obj, arrYielded) {
var arr = arrYielded ? arrYielded : [];
Object.keys(obj).forEach(function(key, idx) {
if (typeof(obj[key]) === 'object') {
searchForProp(lookingFor, obj[key], arr);
} else {
if (key === 'id') {
arr.push(obj[key]);
}
}
});
return arr;
}
var allIDs = searchForProp('id', obj);
console.log(allIDs);
The simplest way that I could think of using lodash:
_(object).map(_.keys).flatten().value();
This of course assumes that the player keys are always at the same level in the object hierarchy.
I am new to javascript. i want to parse this response .
var date=[];
var details=[];
for(post in resulttable.posts){
date=date.concat(resulttable.posts[post].days.Date);
details= details.concat(resulttable.posts[post].days.details);
}
I dont know where am missing. please help me , I want those details in one array and dates to be another array.
{
"status": 1,
"count": 2,
"posts": [{
"days": {
"details": [{
"place": "labs",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meeting with team",
"participants": [{
"Name": "KK",
"Designation": "VP, Operations",
"ContactNumber": "111"
}, {
"Name": "MN1",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}],
"Date": ["2017-02-02"]
},
"name": "test"
}, {
"days": {
"details": [{
"place": "India",
"StartTime": "01:00:00",
"EndTime": "03:00:00",
"Description": "Agenda1",
"participants": [{
"Name": "Kk",
"Designation": "VP, Operations",
"ContactNumber": "11111"
}, {
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "microsoft",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meet CEO",
"participants": [{
"Name": "VR",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "microsoft",
"StartTime": "01:00:00",
"EndTime": "02:00:00",
"Description": "Meet CEO",
"participants": [{
"Name": " VR",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "Formule",
"StartTime": "10:50:00",
"EndTime": "11:50:00",
"Description": "Meet Rajesh",
"participants": [{
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "111"
}]
}, {
"place": "Dell",
"StartTime": "04:00:00",
"EndTime": "08:00:00",
"Description": "Agenda 2",
"participants": [{
"Name": "MN",
"Designation": "Project Lead",
"ContactNumber": "1111111"
}]
}],
"Date": ["2017-02-03"]
},
"name": "test"
}]
}
Check this fiddle
var details = new Array();
var dates = new Array();
for (var i = 0; i < resulttable.posts.length; i++) {
dates = dates.concat(resulttable.posts[i].days.Date);
details = details.concat(resulttable.posts[i].days.details);
}
console.log(details);
console.log(dates);
Or
var details = new Array();
var dates = new Array();
for (post of resulttable.posts) {
dates = dates.concat(post.days.Date);
details = details.concat(post.days.details);
}
console.log(details);
console.log(dates);
I have two collections A and B. ( A,B have the exactly same structure, but different nodes values)
now I'd like to add A into B, with exactly the order: A B and without merging or changing any
nodes inside them. ( just like a joint action) { A } + {B}
I've read the documentation for underscore but couldn't find a proper function which gets this job done.
any idea?
==========update with example ========Sample is simplified from a larger structure, if there are errors please let me know=========
var collection1 = [{
"date": "29 January 2014",
"items": [{
"name": "Jack",
"score": 90,
"title": "programmer"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}, {
"date": "28 January 2014",
"items": [{
"name": "Jim",
"score": 30,
"title": "driver"
}, {
"name": "William",
"score": 52,
"title": "worker"
}]
}]
var collect2 = [{
"date": "26 January 2014",
"items": [{
"name": "Marry",
"score": 92,
"title": "teacher"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}]
========expected output==============
[{
"date": "29 January 2014",
"items": [{
"name": "Jack",
"score": 90,
"title": "programmer"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}, {
"date": "28 January 2014",
"items": [{
"name": "Jim",
"score": 30,
"title": "driver"
}, {
"name": "William",
"score": 52,
"title": "worker"
}]
}, {
"date": "26 January 2014",
"items": [{
"name": "Marry",
"score": 92,
"title": "teacher"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}]
I think what you are looking for is simply Array.concat
var foo = ['a','b','c'];
var bar = ['d','e','f'];
var all = foo.concat(bar); // ['a','b','c','d','e','f'];
Use Underscore#extend as : _.extend(collection1, collection2);
DEMO
col1 = { aa: 'aa', cc: 'cc' }; col2 = { bb: 'bb', dd: 'dd' };
_.extend(col1, col2)
console.log(col1);
# Prints Object {aa: "aa", cc: "cc", bb: "bb", dd: "dd"}