Find a pattern in an array [duplicate] - javascript

This question already has an answer here:
Split String using multiple Selectors
(1 answer)
Closed 4 years ago.
I have this array:
resultArray = ["08:30-10:45", "15:15-17:30"]
I would like to write a javascript that reads this array and return true of the timeslots comes between 00:00 - 08:00.I have written below javascript but this seems not working.
for (var i = 0; i < resultArray.length; i++) {
var bar = /^00:^01:^02:^03:^04:^05:^06:^07:^08/;
if (bar.test(resultArray[i])) {
return true
}
};
I would want to exclude value if it is 08:01 and so on and strictly want to check between 00:00-08:00. Can someone help me on this ?

You are probably looking for something like this:
resultArray = resultArray.filter(e => e.test(pattern))

Related

JavaScript to JSON with Keys [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 3 years ago.
I am trying to convert the parameters from my URL (Example 1) to a JSON Object that looks like Example 2. Unfortunately when I use JSON.stringify() it converts it to Example 3. Can anyone please point me in the right direction for splitting this? I am not sure how to get the keys out.
Example 1 - Input
food=apple,banana,peach&store=walmart&customer=John
Example 2 - Desired Output
{"food": ["apple", "banana", "peach"], "store":"walmart", "customer":"John"}
Example 3 - Current Ouput
{"food=apple,banana,peach", "store=walmart", "customer=John"}
Edits:
Forgot "" in food list
What I tried
data = "food=apple,banana,peach&store=walmart&customer=John";
data = JSON.stringify(data);
Using split, split the string appropriately and insert values in the object
var str = 'food=apple,banana,peach&store=walmart&customer=John';
var arr = str.split('&')
var obj = {};
for (let i = 0; i < arr.length; i++) {
var x = arr[i].split('=')
if (i == 0) {
obj[x[0]] = [];
x[1].split(',').forEach(function(y) {
obj[x[0]].push(y)
})
} else
obj[x[0]] = x[1]
}
console.log(obj)

Javascript: Setting all array values to the same [duplicate]

This question already has answers here:
Initializing an Array with a Single Value
(12 answers)
Closed 8 years ago.
I want to make a new array (of X elements) with all the elements set to 0.
X is set before.
Anyone could make the most compact and easy code for that? Thank you for your
help.
Just create a function:
function makeArray(size, defaultValue) {
var arr = [];
for (var i = 0; i < size; i++) arr.push(defaultValue);
return arr;
}
var myArr = makeArray(10, 0);

Strange behavior when add element to array by key [duplicate]

This question already has answers here:
Javascript array length incorrect on array of objects
(3 answers)
Closed 8 years ago.
I don't understand why is array so strange when I add element by key.
For example:
var test = new Array();
test['a'] = 'aaa';
test['b'] = 'bbb';
test.length == 0; // true. Why?
And when I run:
test.map(function(el){
console.log(el);
});
// nothing print. Why?
When I add element with push method everything works OK. Thanks
Arrays in javascript are not associative, you can't set keys like that i.e.
var test = [];
test.push('aaa');
test.push('bbb');
// OR
test[0] = 'aaa';
test[1] = 'bbb';
test.length = 2

Need help understanding how arrays work in javascript for objects [duplicate]

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 8 years ago.
I am having trouble understanding how arrays work in javascript.
Lets say I have an object, car
car = {
moving: false,
wheels: 4
};
Lets say I want to create an array now, of 5 of these cars. How would you do that? I would want to use a for loop to create them, but I have read many pages on js arrays such as http://www.w3schools.com/js/js_arrays.asp and I am still stumped.
I tried doing
carArray = [];
for(int i=0; i < 5; i++)
{
carArray.push(car);
}
However, when the program runs, there is only one car, not 5, and it is at the last entry of carArray.
The word int should be removed. Then the code should work.
carArray = [];
for (i = 0; i < 5; i++) {
car = {
moving: false,
wheels: 4
};
carArray.push(car);
}

Merge Javascript objects, can't seem to find correct way [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 9 years ago.
Hi i've been struggling a bit and i cant seem to find why the methods i found dont work, could be "fixture-0"
First Object array
personformdata[i]: "{"isInvalid":false,"agentRole":"role"}"
Second
address_ids[i] : "[{"address_id": "fixture-0" }]"
preferable out come, something like this.
"{"isInvalid":false,"agentRole":"role", "address_id": "fixture-0"}"
you can do it like:
var mergedObj={};
for(var key in personformdata[i])
mergedObj[key]=personformdata[i][key];
for(var key in address_ids[i])
mergedObj[key]=address_ids[i][key];
or if you use jQuery:
var mergedObj={};
$.extend(mergedObj, personformdata[i], address_ids[i]);
I myself usually use vanilla JavaScript and prefer not using jQuery.
you can use :
var object = $.extend({}, object1, object2);
for xample you can visit this url.
http://api.jquery.com/jQuery.extend/
If you want to create a new array with merged objects, just do this with a for loop:
var newData = { };
for (var i = 0; i < personformdata.length; i++)
{
newData[i] = {
isInvalid : personformdata[i].isInvalid,
agentRole : personformdata[i].agentRole,
address_id : address_ids[i].address_id
};
}

Categories