Combine Object base on object property in array on objects - javascript

I just want to merge the objects on the base of one of the properties also want to add missing Days name in the output.
Take this for an example:
var array = [
{
"heure1": "14:00",
"heure2": "17:00",
"day": "Sunday",
},
{
"heure1": "08:00",
"heure2": "13:00",
"day": "Sunday",
},
{
"heure1": "14:00",
"heure2": "16:00",
"day": "Monday",
},
{
"heure1": "08:00",
"heure2": "18:00",
"day": "Monday",
},
];
Expected result:
var array = [
{
"heure": ["14:00","17:00","08:00","13:00"],
"day": "Sunday",
},
{
"heure": ["14:00","16:00","08:00","18:00"],
"day": "Monday",
},
{
"heure": [],
"day": "Saturday",
},
{
"heure": [],
"day": "Friday",
},
{
"heure": [],
"day": "Thursday",
},
{
"heure": [],
"day": "Wednesday",
},
{
"heure": [],
"day": "Tuesday",
},
];
Im also trying some of stack overflow answers but Not getting success :-(
Order of the day dose not matter.
Thanks in advance.

My try
<script>
var array = [{
"heure1": "14:00",
"heure2": "17:00",
"DAY": "Sunday",
},
{
"heure1": "08:00",
"heure2": "13:00",
"DAY": "Sunday",
},
{
"heure1": "14:00",
"heure2": "16:00",
"DAY": "Monday",
},
{
"heure1": "08:00",
"heure2": "18:00",
"DAY": "Monday",
},
];
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday", "Saturday"];
var result = [];
days.map(function (day) {
var daysInArray = array.filter(function (a) {
return day == a.DAY
})
// console.log(daysInArray);
if (daysInArray.length) {
time = [];
daysInArray.map(function (item, i) {
for (var key in item) {
if (daysInArray[0].hasOwnProperty(key) && key != "DAY") {
time.push(item[key])
}
}
})
result.push({
"day": day,
"heure": time
})
} else {
result.push({
"day": day,
"heure": []
})
}
})
console.log(result);
</script>

You could first create the 7 entries for the 7 days of the week, each with an empty array for the heure property.
Then iterate the original data, look up the right entry, and push the two times to the heure array.
Note that your Day property has different spellings in your example input (DAY, Day). I would strongly suggest to use all lowercase for such property names.
Here is an implementation:
var array = [{"heure1": "14:00","heure2": "17:00","day": "Sunday",}, {"heure1": "08:00","heure2": "13:00","day": "Sunday",}, {"heure1": "14:00","heure2": "16:00","day": "Monday",}, {"heure1": "08:00","heure2": "18:00","day": "Monday", },];
let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
let obj = Object.fromEntries(days.map(day => [day, { heure: [], day }]));
for (let {heure1, heure2, day} of array) obj[day].heure.push(heure1, heure2);
let result = Object.values(obj);
console.log(result);

my way...
var arr_1 =
[ { heure1: '14:00', heure2: '17:00', day: 'Sunday' }
, { heure1: '08:00', heure2: '13:00', day: 'Sunday' }
, { heure1: '14:00', heure2: '16:00', day: 'Monday' }
, { heure1: '08:00', heure2: '18:00', day: 'Monday' }
]
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const res = days.map(d=>
{
let r = { heure:[], day:d }
arr_1.filter(x=>x.day===d)
.forEach(({heure1,heure2})=> { r.heure.push(heure1,heure2) })
r.heure.sort()
return r
})
console.log( res )
.as-console-wrapper { max-height: 100% !important; top: 0; }

Is that result structure necessary?
If not, modifying the result structure, you could do something like this:
const getHeureByDay = (heureArray) => {
let result = {
Sunday: { heure: [] },
Monday: { heure: [] },
Tuesday: { heure: [] },
Wednesday: { heure: [] },
Thursday: { heure: [] },
Friday: { heure: [] },
Saturday: { heure: [] },
};
heureArray.forEach((heureItem) => {
Object.keys(heureItem).forEach((key) => {
if (key !== "day") {
result[heureItem.day].heure.push(heureItem[key]);
}
})
});
return result;
};
const heureArray = [
{
"heure1": "14:00",
"heure2": "17:00",
"day": "Sunday",
},
{
"heure1": "08:00",
"heure2": "13:00",
"day": "Sunday",
},
{
"heure1": "14:00",
"heure2": "16:00",
"day": "Monday",
},
{
"heure1": "08:00",
"heure2": "18:00",
"day": "Monday",
}
];
console.log(getHeureByDay(heureArray));

Related

how to group the data by a field into subarray in JavaScript

I have an array like below. I want to group this array by day field.
input_array = [{
"name": "alagu",
"day": "monday",
"time": "morning",
"task": "studying"
}, {
"name": "alagu",
"day": "monday",
"time": "evening",
"task": "playing"
}, {
"name": "alagu",
"day": "monday",
"time": "night",
"task": "sleeping"
}, {
"name": "alagu",
"day": "sunday",
"time": "morning",
"task": "playing"
}, {
"name": "alagu",
"day": "sunday",
"time": "evening",
"task": "playing"
}, {
"name": "alagu",
"day": "sunday",
"time": "night",
"task": "sleeping"
}]
I want the result will be like the below array.
result_array = [{
"name": "alagu",
"day": "monday",
"schedule": [
{ "time": "morning", "task": "studying" },
{ "time": "evening", "task": "playing" },
{ "time": "evening", "task": "sleeping" }
]
}, {
"name": "alagu",
"day": "sunday",
"schedule": [
{ "time": "morning", "task": "playing" },
{ "time": "evening", "task": "playing" },
{ "time": "night", "task": "sleeping" }
]
}]
What I mean is I want to group by day and then I have to make sub array schedule having elements of time and task. please help.
For get your required result from sample data by foreach and map functions :
const input_array = [{ "name":"alagu", "day":"monday", "time":"morning", "task":"studying" },
{ "name":"alagu", "day":"monday", "time":"evening", "task":"playing" },
{ "name":"alagu", "day":"monday", "time":"night", "task":"sleeping" },
{ "name":"alagu", "day":"sunday", "time":"morning", "task":"playing" },
{ "name":"alagu", "day":"sunday", "time":"evening", "task":"playing" },
{ "name":"alagu", "day":"sunday", "time":"night", "task":"sleeping" }]
const result = {};
inputArray.forEach(item => {
const { name, day, time, task } = item;
if (!result[day]) {
result[day] = { name, day, schedule: [] };
}
result[day].schedule.push({ time, task });
});
const resultArray = Object.keys(result).map(key => result[key]);
console.log(resultArray);
Result :
[
{
"name":"alagu",
"day":"monday",
"schedule":[
{
"time":"morning",
"task":"studying"
},
{
"time":"evening",
"task":"playing"
},
{
"time":"night",
"task":"sleeping"
}
]
},
{
"name":"alagu",
"day":"sunday",
"schedule":[
{
"time":"morning",
"task":"playing"
},
{
"time":"evening",
"task":"playing"
},
{
"time":"night",
"task":"sleeping"
}
]
}
]
You can use reduce function like this:
const input_array = [{
"name": "alagu",
"day": "monday",
"time": "morning",
"task": "studying"
}, {
"name": "alagu",
"day": "monday",
"time": "evening",
"task": "playing"
}, {
"name": "alagu",
"day": "monday",
"time": "night",
"task": "sleeping"
}, {
"name": "alagu",
"day": "sunday",
"time": "morning",
"task": "playing"
}, {
"name": "alagu",
"day": "sunday",
"time": "evening",
"task": "playing"
}, {
"name": "alagu",
"day": "sunday",
"time": "night",
"task": "sleeping"
}]
const result_array = Object.entries(input_array.reduce((acc, curr) => {
return {
...acc,
[curr.day]: curr.day in acc ? [...acc[curr.day], curr] : [curr]
}
}, {})).map(([key, value]) => ({
name: value[0].name,
day: key,
schedule: value.map(val => ({
time: val.time,
task: val.task
}))
}))
console.log(result_array)
const data = [{"name":"alagu","day":"monday","time":"morning","task":"studying"},{"name":"alagu","day":"monday","time":"evening","task":"playing"},{"name":"alagu","day":"monday","time":"night","task":"sleeping"},{"name":"alagu","day":"sunday","time":"morning","task":"playing"},{"name":"alagu","day":"sunday","time":"evening","task":"playing"},{"name":"alagu","day":"sunday","time":"night","task":"sleeping"}]
let t, r = [...new Set(data.map(i=>i.day))]
.map(day=>(t=data.filter(i=>i.day===day),
{name: t[0].name, day, schedule: t.map(({name, day, ...o})=>o)}))
console.log(r)
I would personally introduce a general purpose groupBy() helper, that allows you to group a collection based on a key.
function groupBy(iterable, fn) {
const groups = new Map();
for (const item of iterable) {
const key = fn(item);
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(item);
}
return groups;
}
With this helper defined we group the data using:
const groups = groupBy(input_array, item => JSON.stringify([item.name, item.day]));
The reason I use JSON.stringify() is because two arrays (objects) are only only equal to each other if they reference the same array. Therefore [item.name, item.day] will never match an existing key, since we just created the array instance. If we serialize the array (using JSON) we can compare 2 strings instead. Which are equal if they contain the same content.
// normal array comparison
["alagu", "monday"] === ["alagu", "monday"] //=> false
// serialized (using JSON) array comparison
'["alagu","monday"]' === '["alagu","monday"]' //=> true
After we've grouped the data, the groups structure looks like this:
// using object notation to display Map instance contents
Map{
'["alagu","monday"]': [
{ name: "alagu", day: "monday", time: "morning", task: "studying" },
{ name: "alagu", day: "monday", time: "evening", task: "playing" },
{ name: "alagu", day: "monday", time: "night" , task: "sleeping" },
],
'["alagu","sunday"]': [
{ name: "alagu", day: "sunday", time: "morning", task: "playing" },
{ name: "alagu", day: "sunday", time: "evening", task: "playing" },
{ name: "alagu", day: "sunday", time: "night" , task: "sleeping" },
],
}
We can then convert this into an array using Array.from(groups) and map() over the key/value pairs to transform them into the desired output.
const result_array = Array.from(groups).map(([json, items]) => {
const [name, day] = JSON.parse(json);
const schedule = items.map(({ time, task }) => ({ time, task }));
return { name, day, schedule };
});
const input_array = [
{ name: "alagu", day: "monday", time: "morning", task: "studying" },
{ name: "alagu", day: "monday", time: "evening", task: "playing" },
{ name: "alagu", day: "monday", time: "night" , task: "sleeping" },
{ name: "alagu", day: "sunday", time: "morning", task: "playing" },
{ name: "alagu", day: "sunday", time: "evening", task: "playing" },
{ name: "alagu", day: "sunday", time: "night" , task: "sleeping" },
];
const groups = groupBy(input_array, item => JSON.stringify([item.name, item.day]));
const result_array = Array.from(groups).map(([json, items]) => {
const [name, day] = JSON.parse(json);
const schedule = items.map(({ time, task }) => ({ time, task }));
return { name, day, schedule };
});
console.log(result_array);
// groupBy() helper
function groupBy(iterable, fn) {
const groups = new Map();
for (const item of iterable) {
const key = fn(item);
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(item);
}
return groups;
}
In the above code we turn the serialized key back into a JavaScript structure using JSON.parse(), this allows us to grab the name and day of the current group.
We then need to transform items—an array of objects that still contain all the properties—into an array of objects that only have the time and task property.
.map(({ time, task }) => ({ time, task })) does exactly that. It is a combination of object destructuring and the property definition shorthand.

How to merge two objects with same property in Node JS

I have a JSON Data like this:
"Data": [
{
"time": "18:40:43",
"count": 7,
"endTime": "15:46:25",
"date": "2019-01-16",
"dow": "Thursday"
},
{
"count": 11,
"time": "16:39:52",
"endTime": "19:41:03",
"dow": "Thursday",
"date": "2019-01-16"
},
]
I want to merge two objects in this array, but it have same properties like date, dow
at the end I want to represent data like this:
"Data": [
{
"time": "16:39:52",
"count": 18,
"date": "2019-01-16",
"dow": "Thursday"
"endTime": "19:41:03",
},
]
time: should be least from both objects and endTime should be largest of both of them
count should be sum of both. date and dow is common in both objects
How can I merge these object in this way in node JS?
const data=[
{
"time": "18:40:43",
"count": 7,
"endTime": "15:46:25",
"date": "2019-01-16",
"dow": "Thursday"
},
{
"count": 11,
"time": "16:39:52",
"endTime": "19:41:03",
"dow": "Thursday",
"date": "2019-01-16"
},
];
let date=(time)=>new Date().setHours(...time.split(":"));
let newData=[];
data.forEach((item)=>{
let findItem=newData.findIndex((e)=>e.date===item.date && e.dow===item.dow);
if(findItem!=-1){
let find=data.filter((e)=>e.dow===item.dow && e.date===item.date);
let time=find.filter((i)=>find.find(i2=>date(i2.time)>date(i.time)));
let endTime=find.filter((i)=>find.find(i2=>date(i2.endTime)<date(i.endTime)));
item.endTime=endTime?.[0]?.endTime || item?.endTime;
item.time=time?.[0]?.time || item?.time;
item.count=find.map((e)=>e.count).reduce((partialSum, a) => partialSum + a, 0);
let findItem=newData.findIndex((e)=>e.date===item.date && e.dow===item.dow);
if(findItem!=-1) newData.splice(findItem,1);
newData.push(item);
}
else newData.push(item);
});
console.log(newData);
Here's a simple and readable answer:
const data = [
{
time: "18:40:43",
count: 7,
endTime: "15:46:25",
date: "2019-01-16",
dow: "Thursday",
},
{
count: 11,
time: "16:39:52",
endTime: "19:41:03",
dow: "Thursday",
date: "2019-01-16",
},
];
const mergeObjects = (data) => {
mergedObj = { ...data[0] };
for (i = 1; i < data.length; i++) {
const obj = data[i];
for (const key in obj) {
switch (key) {
case "count":
mergedObj.count = (mergedObj.count || 0) + obj.count;
case "time":
mergedObj.time =
mergedObj.time < obj.time ? mergeObjects.time : obj.time;
case "endTime":
mergedObj.endTime = mergedObj.endTime > obj.endTime ? mergeObjects.endTime : obj.endTime;
}
}
}
return mergedObj;
};
console.log(mergeObjects(data));
output
{
"time": "16:39:52",
"count": 18,
"date": "2019-01-16",
"dow": "Thursday"
"endTime": "19:41:03",
},
Assuming
"Data" would have only two objects inside it
time would be in "HH::MM::SS" format
Pass the values onto constructJSON utility function which would return the formatted value
var Data = [
{
"time": "18:40:43",
"count": 7,
"endTime": "15:46:25",
"date": "2019-01-16",
"dow": "Thursday"
},
{
"count": 11,
"time": "16:39:52",
"endTime": "19:41:03",
"dow": "Thursday",
"date": "2019-01-16"
},
]
function constructJSON(data)
{
const returnData = {}
returnData['count'] = data[0].count + data[1].count
returnData['date'] = data[0].date // since its common for both
returnData['dow'] = data[0].dow // since its common for both
returnData['time'] = parseInt(data[0].time.split(':').join(''),10) < parseInt(data[1].time.split(':').join(''),10) ? data[0].time:data[1].time;
returnData['endTime'] = parseInt(data[0].endTime.split(':').join(''),10) > parseInt(data[1].endTime.split(':').join(''),10) ? data[0].endTime:data[1].endTime;
return returnData
}
console.log(constructJSON(Data))
If you use Array.reduce, you should be able to do this pretty easily, it should also expand to let you expand with more than just 2 objects in the array.
const data = [{
"time": "18:40:43",
"count": 7,
"endTime": "15:46:25",
"date": "2019-01-16",
"dow": "Thursday"
},
{
"count": 11,
"time": "16:39:52",
"endTime": "19:41:03",
"dow": "Thursday",
"date": "2019-01-16"
},
];
//res will contain the output
const res = data.reduce(function(acc, curr) {
//Initially the accumulator will be null
if (!acc) return curr;
//Add the counts up
acc.count += curr.count;
//Convert both times into dates and find the minimum
const accTime = new Date(acc.date + 'T' + acc.time); //We add the T here since that's just how the Date constructor accepts times
const currTime = new Date(acc.date + 'T' + curr.time);
acc.time = new Date(Math.min(accTime, currTime)).toTimeString().split(' ')[0]; //See https://stackoverflow.com/questions/19346405/how-to-get-hhmmss-from-date-object
//Do the same for the end times but find the maximum
const accEndTime = new Date(acc.date + 'T' + acc.endTime);
const currEndTime = new Date(acc.date + 'T' + curr.endTime);
acc.endTime = new Date(Math.max(accEndTime, currEndTime)).toTimeString().split(' ')[0];
return acc;
});
console.log(res);
Another way of merging - all keys are combined, the common keys are overwritten by the second object, but count, time and endTime are calculated by special conditions
const
data = [{"time": "18:40:43","count": 7,"endTime": "15:46:25","date": "2019-01-16","dow": "Thursday"},{"count": 11,"time": "16:39:52","endTime": "19:41:03","dow": "Thursday","date": "2019-01-16"}],
[first, last] = data,
dumbDate = '2000-01-01 ',
result = {
...first,
...last,
count: first.count + last.count,
time: (new Date(`${dumbDate}${first.time}`) < new Date(`${dumbDate}${last.time}`)) ? first.time : last.time,
endTime: (new Date(`${dumbDate}${first.endTime}`) > new Date(`${dumbDate}${last.endTime}`)) ? first.endTime : last.endTime,
};
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
Use Array#reduce() and Array#map() methods as follows:
const data = {"Data": [{"time": "18:40:43", "count": 7, "endTime": "15:46:25", "date": "2019-01-16", "dow": "Thursday"}, {"count": 11, "time": "16:39:52", "endTime": "19:41:03", "dow": "Thursday", "date": "2019-01-16" }]};
const newD = {Data:data.Data.reduce(
(prev,{date,dow,count,...r}) =>
//check if an element in array prev has dow & date props equal to the current element
prev.findIndex(p => p.date === date && p.dow === dow) > -1 ?
//If found process the matching element
prev.map(({date:d,dow:w,count:c,...rs}) =>
d === date && w === dow ?
({...rs,date:d,dow:w,count:c+count,time:rs.time < r.time ? rs.time : r.time,endTime:rs.endTime > r.endTime ? rs.endTime : r.endTime}) :
({...rs,date:d,dow:w,count:c})
) :
//If not add the current element to array prev
prev.concat({...r,date,dow,count}),
//start off with an empty array
[]
)};
console.log( newD );

Combining objects in array based on key values [duplicate]

This question already has answers here:
How to merge objects if certain keys and values match in JavaScript?
(4 answers)
Closed 2 years ago.
I have an array of objects with a common key. I would like to combine all the objects within the array based on key values.
My array:
let array = [
{
"day": "Monday",
"item1": "abc"
},
{
"day": "Tuesday",
"item1": "def"
},
{
"day": "Monday",
"item2": "123"
},
{
"day": "Tuesday",
"item2": "456"
}
]
Based on the example, I want to combine the objects within my array based on the common day key values and merge the rest of the other key value pairs (the rest of the keys are dynamic).
Expected output:
[
{
"day": "Monday",
"item1": "abc",
"item2": "123"
},
{
"day": "Tuesday",
"item1": "def",
"item2": "456"
}
]
I have tried using Lodash and plain ES6 but still unable to come up with a solution for it.
You can use lodash reduce function, like this:
let array = [
{
"day": "Monday",
"item1": "abc"
},
{
"day": "Tuesday",
"item1": "def"
},
{
"day": "Monday",
"item2": "123"
},
{
"day": "Tuesday",
"item2": "456"
}
]
let merged = _.reduce(array, function(result, value, key) {
let item = result.find(item => item.day === value.day);
if (!item) {
result.push(value)
} else {
_.merge(item, value);
}
return result;
} , []);
Try using Array#reduce , Spread Operator and approach with ES6
const arr = [ { "day": "Monday", "item1": "abc" }, { "day": "Tuesday", "item1": "def" }, { "day": "Monday", "item2": "123" }, { "day": "Tuesday", "item2": "456" } ];
let res = arr.reduce((acc,{day,...rem})=>( acc[day] = acc[day] ? {...acc[day],...rem} : {day,...rem},acc),{});
console.log(Object.values(res))
Found the solution
_(array).groupBy('day').map(_.spread(_.assign)).value()

how to remove two json objects with same id

If I have a .json file with the following contents:
[
{
"id": "1234",
"day": "Monday",
"course": "Math110",
},
{
"id": "1234",
"day": "Wednesday",
"title": "Math110",
},
{
"id": "1345",
"day": "Tuesday",
"title": "Economics210",
}
]
How can I remove all the objects with id "1234" in Javascript? (note there are two objects with the same ID)
Would delete["1234"] work?
Use .filter to filter out elements of an array:
const input = [{
"id": "1234",
"day": "Monday",
"course": "Math110",
},
{
"id": "1234",
"day": "Wednesday",
"title": "Math110",
},
{
"id": "1345",
"day": "Tuesday",
"title": "Economics210",
}
];
const output = input.filter(({ id }) => id !== '1234');
console.log(output);
You cannot use delete operator because its for deleting a property in an object. You actually want to delete an object inside an array.
Use the good old for loop and Array.splice():
var inputArray = [{
"id": "1234",
"day": "Monday",
"course": "Math110",
},
{
"id": "1234",
"day": "Wednesday",
"title": "Math110",
},
{
"id": "1345",
"day": "Tuesday",
"title": "Economics210",
}
];
var deleteId = "1234";
for (var i = 0; i < inputArray.length; i++) {
if (inputArray[i].id === deleteId) {
inputArray.splice(i, 1);
i = i-1;
}
}
console.log(inputArray);
Note: If you are fine with creating another array and not modifying the existing array, use Array.filter() as mentioned in the other answers.
You can parse the JSON to an array and then use Array.prototype.filter to get a new array which doesn't have the objects with id "1234":
const json = '[{"id":"1234","day":"Monday","course":"Math110"},{"id":"1234","day":"Wednesday","title":"Math110"},{"id":"1345","day":"Tuesday","title":"Economics210"}]';
const result = JSON.parse( json ).filter( obj => obj.id !== "1234" );
console.log( JSON.stringify( result, null, ' '.repeat(8) ) );
You have to delete by specifying the position of the item in the array:
const arr = [{
"id": "1234",
"day": "Monday",
"course": "Math110",
},
{
"id": "1234",
"day": "Wednesday",
"title": "Math110",
},
{
"id": "1345",
"day": "Tuesday",
"title": "Economics210",
}
];
arr.forEach(function(item, i){
if(item.id == "1234")
delete arr[i];
});
console.log(arr.filter(j => j))
You can use filter method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
items = [
{
"id": "1234",
"day": "Monday",
"course": "Math110",
},
{
"id": "1234",
"day": "Wednesday",
"title": "Math110",
},
{
"id": "1345",
"day": "Tuesday",
"title": "Economics210",
}
];
items.filter(function(item) {
return item.id !== '1234';
});

jquery print object content

I'm new to jquery and want to print all times from monday only (or day == today later on)
I manage to print all times of the object and not 1 day I pick, maybe my iteration is wrong, would like any hints.
var arr = [{
"dateString": "2016-12-05",
"weekday": "mon",
"opening_hours": [{
"from": "09:00",
"to": "15:00",
"fromNumber": 9,
"toNumber": 15
}, {
"from": "17:00",
"to": "20:00",
"fromNumber": 17,
"toNumber": 20
}]
}, {
"dateString": "2016-12-06",
"weekday": "tue",
"opening_hours": [{
"from": "09:00",
"to": "17:00",
"fromNumber": 9,
"toNumber": 17
}]
}, {
"dateString": "2016-12-07",
"weekday": "wed",
"opening_hours": [{
"from": "09:00",
"to": "20:00",
"fromNumber": 9,
"toNumber": 20
}]
}, {
"dateString": "2016-12-08",
"weekday": "thu",
"opening_hours": [{
"from": "1:00",
"to": "11:00",
"fromNumber": 1,
"toNumber": 11
}]
}];
$("button").click(function() {
var today = new Date();
var str = today.toString('dddd, MMMM ,yyyy')
var sta = str.substring(0, 4);
console.log(sta);
$.each(arr, function(i, currProgram) {
$.each(currProgram.opening_hours, function(key, val) {
// $.each(num, function (axa, dasa) {
if (currProgram.weekday == "mon") {
console.log("monday");
timefrom = (val.from);
timeto = (val.to);
} else {
console.log("no monday");
}
// console.log(num[axa]);
$(".result").append("\ntime: " + timefrom + " to " + timeto + "\n");
console.log("time: " + timefrom + " to " + timeto + "\n");
// });
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="calnote">
<day></day>
<br>
<month></month>
<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>
<div id="div1">
<h2>click button for ajax</h2></div>
<div class="result">
<br>
</div>result</div>
<br>
<div class="result1">
</div>result1</div>
<p class="calnote">
<day></day>
<br>
<month></month>
<blackcorner>
<em>
<time></time>
</em>
</blackcorner>
</p>
click event
<button class="abc">Click me</button> here
jsfiddle
The result should print only:
time: 09:00 to 15:00
time: 17:00 to 20:00
You might want to move the logic console.log("time: " + timefrom + " to " + timeto + "\n"); into the if block
var arr = [{
"dateString": "2016-12-05",
"weekday": "mon",
"opening_hours": [{
"from": "09:00",
"to": "15:00",
"fromNumber": 9,
"toNumber": 15
}, {
"from": "17:00",
"to": "20:00",
"fromNumber": 17,
"toNumber": 20
}]
}, {
"dateString": "2016-12-06",
"weekday": "tue",
"opening_hours": [{
"from": "09:00",
"to": "17:00",
"fromNumber": 9,
"toNumber": 17
}]
}, {
"dateString": "2016-12-07",
"weekday": "wed",
"opening_hours": [{
"from": "09:00",
"to": "20:00",
"fromNumber": 9,
"toNumber": 20
}]
}, {
"dateString": "2016-12-08",
"weekday": "thu",
"opening_hours": [{
"from": "1:00",
"to": "11:00",
"fromNumber": 1,
"toNumber": 11
}]
}];
function buttonClick() {
var today = new Date();
var str = today.toString('dddd, MMMM ,yyyy')
var sta = str.substring(0, 4);
$.each(arr, function(i, currProgram) {
$.each(currProgram.opening_hours, function(key, val) {
if (currProgram.weekday == "mon") {
//console.log("monday");
timefrom = (val.from);
timeto = (val.to);
console.log("time: " + timefrom + " to " + timeto + "\n");
} else {
//console.log("no monday");
}
});
});
}
buttonClick()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Categories

Resources