Getting the first item in an [word, word] [duplicate] - javascript

This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.

As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]

Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);

If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow

Related

Remove second part of string with split() [duplicate]

This question already has answers here:
How to remove part of a string?
(7 answers)
Closed 10 months ago.
I have a variable with a string, let's say this one, which I then display on a page on the site:
let value = "qwe asd — bensound summer";
document.getElementById("text").innerHTML = value;
And I want to remove its second part when displaying this line on the page bensound summer along with a dash —.
And in order to receive only the first part, which is before the dash, when displayed on the page, in the form: qwe asd.
I read about str.split() but didn't find anything like it and didn't quite understand how it all works.
Use the split method
let value = "qwe asd - bensound summer";
value = value.split('-')[0] //
document.getElementById("text").innerHTML = value;
You can use it like this:
let value = "qwe asd — bensound summer";
let splittedString = value.split("-");
document.getElementById("text").innerHTML = splittedString[0];
Hope, it helps!!

How to make an array by one string in JS [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 2 years ago.
I get one string by query like '5e6,5e4,123'.
And I want to make an array containing this query as below in JS.
['5e6', '5e4', '123']
How can I make this? Thank you so much for reading it.
You can use .split(',')
var str = "5e6,5e4,123";
var array = str.split(',');
console.log(array);
You can read more on this here
Use String.split:
console.log('5e6,5e4,123'.split(","))
var query = '5e6,5e4,123';
var queries = query.split(‘,’);
You can make use of split method of string like below:
var res = str.split(',');
const output = input.split(',');

Query syntax for JSON lookup data with var ( not condition ) [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'm trying to filter some JSON data, but I would like the lookup to be based on selection of a drop down. However, I just can't get the syntax correct when trying to do this. Currently the following works in my code, great:
var as = $(json).filter(function (i, n) {
return (n.FIELD1 === "Yes"
});
However, what I would like to do is replace the FIELD1 value with a var from the drop down. Something like this following, which is not working:
var dropdownResult = "FIELD1";
var as = $(json).filter(function(i, n) {
return (n.dropdownResult === "Yes"
});
I'm trying to get the var to become the field name after the n. but it's not working.
Thanks for your time. Sorry if this has been answered many times before and is obvious to you.
To use a variable value as the key of an object you should use bracket notation, like this:
var dropdownResult = "FIELD1";
var as = $(json).filter(function(i, n) {
return n[dropdownResult] === "Yes";
});
I removed the extraneous ( you left in your code - I presume this was just a typo as it would have created a syntax error and stopped your code from working at all.
Also note that it's much better practice to use a boolean value over a string 'Yes'/'No'

Javascript - delete array items by value LIKE [duplicate]

This question already has answers here:
remove all items in array that start with a particular string
(5 answers)
Closed 7 years ago.
I have this simple array
var array = ['x.89999', 'y.sisisis', 'x.585858'];
I want to remove all the items in the array starting by 'x.' so to return this array:
['y.sisisis']
How can i do this without having to loop or iterate all the entire array? (i know it's probably not possible so don't mind if not possible)
Is there some builtin / native code i can use?
Thanks
you may use array.filter()
var newArray = array.filter(function(item){
return item.indexOf('x.') !== 1;
});
there is no way to do this job without looping through the whole array.
The only case – array is sorted alphabetically. But sorting demands looping through too
Assuming that the items to be removed will always precede the other items alphabetically (ie x is before y), you can sort your array and then break the loop as soon as the non-x value has been found:
function filter(arr) {
arr.sort();
for (var i = 0, l = arr.length; i < l; i++) {
if (arr[i][0] !== 'x') {
break;
}
}
return arr.slice(i);
}
filter(arr); // logs: "Iterated to 5 of array length 9"
DEMO
Of course, as JazzCat mentions, this is no good if you only have one y value and it's right at the end of the array - you're still going to have to iterate over the whole array.
Alternate option is to use regular expression, something like this. Fix your regex according to your requirement.
var array = ['x.89999', 'y.sisisis', 'x.585858'];
var filtArr = array.filter(function(x){
return x.match("^[x].+$");
});
console.log(filtArr);
One different answer not using explicit iteration or Array.prototype.filter:
var array = ['x.89999', 'y.sisisis', 'x.585858'];
array = array.join("#").replace(/(#?x\.\w*)/g,"").replace(/^#/,"").split("#");
//["y.sisisis"]
Of course you will need to take care of the separator character.
The good point is that it works on old browsers (ie8) while Array.prototype.filter does not.

Get the last occurrence of string within another string [duplicate]

This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found

Categories