check if a string is part of an array - javascript [duplicate] - javascript

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to be able to see if "megan" is a part of the array people. Though, i can only do this by using people[2].
Here is my code:
var people = new Array("jack","ian");
document.write(people[0]);
people.push("megan");
document.write("<br />");

Use the indexOf method of the array:
if(people.indexOf("megan") > -1) {
//do stuff
} else {
//not in array
}
If the string is in the array, 0 is returned. if not, -1 is returned.

Related

JavaScript hash of Arrays [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 years ago.
I have such type of array
How to access Viena
There could be a lot of cities
[{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"},{..},{..}],{"Paris":[{..}]} ]
You can use Array.filter for this.
var data = [{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]},{"Paris":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]}];
var result = data.filter(function(value){return Object.keys(value).indexOf("Viena") != -1;});
console.log(result[0]);

Object key name with dash [duplicate]

This question already has answers here:
Which characters are valid/invalid in a JSON key name?
(4 answers)
Closed 5 years ago.
I have to make an object with - like:
let myObject = {
one-property: 'assignee'
}
but JavaScript does not allow this. So any trick to make this work with -? My whole backend has objects with - on keys.
You have to put the key in quotes
let myObject = {
'one-property': 'assignee'
}

How to iterate array of objects [duplicate]

This question already has answers here:
Object.length undefined in javascript [duplicate]
(3 answers)
JavaScript object literal length === undefined?
(5 answers)
Closed 5 years ago.
Here is my jsfiddle
Here is my array looks like
var arr = [];
arr['alpha'] = {'a':'1','b':'1'};
arr['beta'] = {'a':'2','b':'4'};
console.log(arr)
When i take console.log(arr.length) It says 0. So i can't able to for loop this one
How can i iterate this array ?
Note :
I don't want to use jquery, i prefer only javascript

JavaScript - check length value [duplicate]

This question already has answers here:
JavaScript get element by name [duplicate]
(8 answers)
Closed 7 years ago.
I would like to create a form, in this form I have to validate if the value is long enough.... More than 1 character.
My actual code looks like that:
function validateForm(){
alert("Form ok");
if(document.getElementsByName("firstname").value.length >1){
alert("if");
}
else{
document.getElementById("nameValidation").innerHTML= "* You must enter a first name";
alert("else");
}
I am struggling to understand why it doesn't work....
Many thanks.
You're using document.getElementsByName which returns NodeList. So, you should use it like this:
document.getElementsByName("firstname")[0].value.length > 1
getElementsByName returns array (or array-like object) of elements. You can access each element by index, e.g.:
document.getElementsByName("firstname")[0].value.length

Accessing an array within an object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
var oldMatches = Meteor.users.find(userId, {
fields: {
_id: false,
matches: true
}
}).fetch()
This returns an object with an empty matches array, as it should. How would I access this array to find the length of it?
Thanks.
You would find the length of the array with
oldMatches.length
So:
if(oldMatches.length) {
// is not empty
} else {
// is empty
}
N.B. length only works on a returned array from fetch, not a Meteor cursor

Categories