Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
var y = [1,2,3];
(y.indexOf(1) > -1) ? console.log(true) : '';
why above code don't work? I thought it's possible to check 1 in y which is an array?
indexOf is zero based, as all indices of arrays and strings. If an item is not in the array, then it return -1, otherwise it returns the index of the item.
var y = [1, 2, 3];
console.log(y.indexOf(1) !== -1);
It's because in JS indexes starts at 0th place.
So 1 is in 0th place, 2 is in 1st place and so on
indexOf returns the first matching position of the value in the array.
1 appears in position 0.
0 > 1 is false.
You need to test if the position is greater than -1 to see if it appears in the array at all.
Since writing this answer, the question has been edited to use -1. The code in the question now works as desired.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I have 3 arrays. Lets say the first array has 3 elements, second array has 2 elements, and third array has 5 elements. When I concatenate them the array[3] will go to first element of the second array, array[6] will go to the second element of third array, because I first concatenate the first array with the second. If I concatenate the first array with the third, then concatenate the second array, array[3] will point to the first element of the third array.
don't understand your content, but according do your subject , I think below may help.
const a = [1,2,3]
const b = [4,5]
const c = [...a, ...b] // [1,2,3,4,5]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I know nothing about JavaScript.
Assume that v contains a list of positive integers, vi is an index value, say current vi = 0.
I would like to know how to convert v.splice(vi, 1) to Golang
Is .splice() is equivalent to slices?
v.splice(vi, 1) removes 1 element from vi. To do the same in go, you can do:
append(v[:vi],v[vi+1:]...)
That is, first get the slice up to vi, then add all the elements after vi.
From : https://www.w3schools.com/jsref/jsref_splice.asp
Syntax
array.splice(index, howmany, item1, ....., itemX)
Parameter: index
Description. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array.
Parameter :howmany(Optional)
Description: The number of items to be removed. If set to 0, no items will be removed
Parameter:item1, ..., itemX (Optional)
Description: The new item(s) to be added to the array
You may visit https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice for trying it yourself
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
"addresses" is an array of objects, each object element is having specific _id.
I'm trying to find .indexOf(req.params.a_id) within idsArray (which is an array of ids of all those object elements within the array), but .indexOf() returns -1 although _id exists in idsArray.
code snapshot!
I think .indexOf is most used in strings, you can get it by this way :
idsArray.filter(x => x.id == req.params.a_id)
this will return an array with the matching element.
reference: https://www.w3schools.com/jsref/jsref_filter.asp
UPDATE:
to find the index you can use
idsArray.findIndex(x=> x == req.params.a_id)
To check if an element exists in an array you can use the some method:
idsArray.some(id => id === req.params.a_id)
which returns true or false if the id exists or not.
Check type of your variables.
Maybe req.params.a_id is string ("1") in your case?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I make an array and enter [a, b, c] in it and then I reverse the array to check either both the array is now different because of reverse order and then get an answer that both the arrays are same and confused about it that on which basis both are same?
reverse does not create a new array. It works in place and returns the same array it has been called on.
Here is a code-example explaining what Daniel Hilgarth sais
var arr = [0, 1, 2];
console.log(arr);
console.log(arr === arr.reverse());
console.log(arr);
As you can see, all you did is reverse the original array, and not get a reversed copy of it
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following code: var a = rfc();
rfc() returns an array expected in following format {usd:1,gbp:0.59,bdt:77.5}. I need to count it. I failed to do that using a.length . It always returns 0. I have checked the contents of a using the function below and the return was as follows:
function to check array contents:
Object.keys(coordinates).forEach(function(key) {
console.log(key, coordinates[key]);
});
Output at console:
usd 1
bdt 77.5
gbp 0.59
undefined
Why can't I get the array length using array.length and How can I count the number of elements of this array with minimum amount of codes?
Your JavaScript:
{usd:1, gbp:0.59, bdt:77.5}
represents an object, not an array. So it doesn't have a length property. To count the number of properties in your object very quickly, you can do:
var a = {usd:1,gbp:0.59,bdt:77.5}
var b = Object.keys(a).length
b will now be set to 3.
Try:
Object.keys(coordinates).length
The original object is an object, not an array. However, in extracting an array of the keys of the object (as you do in Object.keys(coordinates) already), you have an array you can take the length of.
> a = {usd:1,gbp:0.59,bdt:77.5};
[object Object] {
bdt: 77.5,
gbp: 0.59,
usd: 1
}
> Object.keys(a)
["usd", "gbp", "bdt"]
> Object.keys(a).length
3