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
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 2 years ago.
Improve this question
business=[0:{"name":{"en":'prateek'}},1:{"name":{"ar":'rahul'}}]
How I can extract the value of en and ar from this type of the repeted object in a array
The issue with your question is that you define business as an Array with the use of square brackets, but then proceed to use key value pairs directly within the array (via the usage ":"), which is reserved for objects and not arrays. I'd recommend researching both array and object datatypes, however simply put:
let myArray = [1, 2, 3, ...];
// only stores values which can be retrieved using the values index i.e. myArray[0]
let myObj = {"key1" : "value1", "key2" : "value2"};
// values are stored against keys, and can be accessed via the key i.e. myObj.key1
I think you've confused how objects and arrays should work. My guess is that you'd be better off with this structure:
let businesses = [];
businesses.push({enName: 'prateek', arName: 'rahul'});
console.log(businesses[0], businesses[0].enName, businesses[0].arName);
This way you're using an array to hold a collection of businesses and which are represented by objects. These objects in turn have attributes for enName and arName.
I think this would be a much clearer way of structuring your issue.
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have two comma separated lists, the first is a list of possible values, and the second is a list of "selected" values. I need to create a list of all of the items in the first list that do not exists in the second list.
I could just split the first list into an array and use a "for" to go through the list using a string_pos to see if the first list item is contained in the second, but I'm wondering if there is a more efficient way to accomplish this.
Thanks!!
You can filter the possible list.
if the lists are strings, split or match them to get arrays.
var possible=[1,2,3,4],
selected=[2,4];
var unchosen=possible.filter(function(itm){
return selected.indexOf(itm)==-1;
});
unchosen
/* returned value: (Array)
1,3
*/
If you are looking for the best possible way, this is what you have to do
Convert the list to be checked, to an object, in liner time. Because, objects are technically hashtables, which offer faster lookups O(1)).
Then, iterate over the first list and check if the current element is there in the object or not. If it is not there, add it to the result.
var list1 = [1, 2, 3], list2 = [1, 2], dict2 = {};
list2.forEach(function(item) {
dict2[item] = true;
});
var result = list1.reduce(function(prev, current) {
if (dict2.hasOwnProperty(current) === false) {
prev.push(current);
}
return prev;
}, [])
console.log(result);
Output
[ 3 ]
The first thing you want to do is definitely to split the two comma separated lists into arrays of strings. Assume that they are formatted fairly reasonably, you can do this with
possible_values = possible_string.split(/,\s?/) //split on commas with a possible space
selected_values = selected_string.split(/,\s?/)
If you are willing to use outside libraries, underscore.js has a perfect function for this. The operation you are describing is the set difference operator, which is the difference function in underscore.
The result you want is the return value of calling
_.difference(possible_values, selected_values)