How to concatenate two arrays without changing order in javascript [closed] - javascript

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]

Related

Copy first 7 keys in object into a new object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
As the title suggests, how do I make a new object containing the 7 first keys of another object using JS? :) This is the structure of the object I would like to copy the data from.
{"data":[{"id":2338785,"team1":{"id":10531,"name":"Just For Fun"},"team2":{"id":10017,"name":"Rugratz"},"result":"2 - 0","event":{"name":"Mythic Cup 5","id":5148},"format":"bo3","stars":0,"date":1578279271000},....],"last_update":1578329378792}
Let's say there are 100 keys like this one, and I only want to copy the 7 first ones into a new object in JS.
Well technically, you have only 2 keys in the given Object but if you mean the data object, here's what you can do.
const MyNewObject = Object.entries(YourObject)
const results = []
and a simple for loop
MyNewObject.forEach((pair,i) => {
// for the 8th item it breaks and doesn't push it to results array
if ( i === 7 ) break;
results.push(pair)
}
OR u use slice instead :
// a little bit neater , and u call remove the empty results array above
const thisNewResult = MyNewObject.slice(0,6)
and finally the results is an array of key value pairs and you should do this code to make a new object from the results entries
const finalResults = Object.fromEntries(results)
Please notice that this may not be the Order you want since Object.Entries gives you the same order as the order of for in loop (for more info visit Elements order in a "for (… in …)" loop)

Convert from JavaScript v.splice(vi, 1) to Go [closed]

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

How actual array and reverse of it is same in javascript [closed]

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

indexOf method on an array in javascript [closed]

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.

Get list of items in one list that are not in another [closed]

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)

Categories

Resources