add array of string arrays to array of string [duplicate] - node.js

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 4 years ago.
I want to add mongo ids in a final array from my query, here is what i am trying to do
var arr = [];
// array1 = [ "59ae3b20a8473d45d11987f8",
"59bb8a9c8f541e060054580e",
"59ae905f43d38f64d85d3c21"]
//array2 = [
"59bce938cbe9c90271742c1a",
"59bcebb4cbe9c90271742c2e" ]
and when i am adding them as arr.push(array1), arr.push(array2)
then i am getting this result
//arr = [ 59ae3b20a8473d45d11987f8,
59bb8a9c8f541e060054580e,
59ae905f43d38f64d85d3c21,
59bce938cbe9c90271742c1a,
59bcebb4cbe9c90271742c2e ]
in result array the type has changed(from strings) and i want these elements as string as they are in array1 and array2, i tried using concat but not working, please help

Using the concat method, you could proceed as:
arr = [].concat(array1, array2)
or even just arr = array1.concat(array2). Here, the result is assigned to arr since concat does not mutate the original array.
Alternatively, using the spread syntax:
arr = [...array1, ...array2]

array1 = [ "59ae3b20a8473d45d11987f8",
"59bb8a9c8f541e060054580e",
"59ae905f43d38f64d85d3c21"]
array2 = [
"59bce938cbe9c90271742c1a",
"59bcebb4cbe9c90271742c2e" ]
var finalArr = array1.concat(array2);
console.log(finalArr);
array1 = [ "59ae3b20a8473d45d11987f8",
"59bb8a9c8f541e060054580e",
"59ae905f43d38f64d85d3c21"]
array2 = [
"59bce938cbe9c90271742c1a",
"59bcebb4cbe9c90271742c2e" ]
var finalArr = [...new Set([...array1 ,...array2])];
console.log(finalArr);

Related

Elegant way to loop through a list of strings, split each string and output except N first elements

For instance, this list contains the following strings:
array[0]: "text1,text2,text3,text4,text5"
array[1]: "text1,text2,text3,text4,text5,text6,text7"
array[2]: "text1,text2,text3,text4,text5,text6"
I need to ignore the first 3 texts and take the rest.
Output should be:
"text4,text5"
"text4,text5,text6,text7"
"text4,text5,text6"
I suppose I need to split the string using the comma and iterate from 2 till array length for each element.
Is there an elegant way to perform this?
Hi you can use list comprehension
array = ["text1,text2,text3,text4,text5", "text1,text2,text3,text4,text5,text6,text7", "text1,text2,text3,text4,text5,text6"]
result = [i.split(",")[2:] for i in array]
print(result)
For Groovy, you can do something like this:
def input = [
"text1,text2,text3,text4,text5",
"text1,text2,text3,text4,text5,text6,text7",
"text1,text2,text3,text4,text5,text6",
]
def result = input.collect {
it.split(',').drop(3).join(',')
}
result.each { println it }
which prints out
text4,text5
text4,text5,text6,text7
text4,text5,text6
For lovers of 1-liners:
def input = [
"text1,text2,text3,text4,text5",
"text1,text2,text3,text4,text5,text6,text7",
"text1,text2,text3,text4,text5,text6",
]
def output = input*.split(',')*.getAt( 3..-1 )*.join(',')
output.each this.&println
prints:
text4,text5
text4,text5,text6,text7
text4,text5,text6
array = array.collect{ i-> i[3..-1] }
if you want just to print
array.each{ i-> println i[3..-1].join(',') }

Insert character inside array data

how to insert character from array?
This my data :
["a", "b", "c", ...]
i'm wanna change my data like this:
["$a", "$b", "$c", ...]
Thanks before
let a = ["a", "b", "c", ...]
a.map(value => '$'+value) // this will do what you need returns ["$a", "$b", "$c"]
map basically iterate through the array and map each element according to the given condition
Array.map(value => map the value with any type of data here)
Use map - ES6 way
Map mdn
const arr = ['a', 'b']
const modifiedArray = arr.map(el => '$' + el)
console.log(modifiedArray)
However note it will modify the original array so if you don't want to modify original array
Use spread of ES6
spread mdn
const modifiedArray = {...arr}.map(el => '$' + el)
Non ES6
var arr = ['a', 'b'],
modifiedArr = []
for(let i=0; i < arr.length; i++) {
modifiedArr.push('$' + arr[i])
}
console.log(modifiedArr)

get common elements from a result which consists of more than 1 array in arangodb

I want to fetch common elements from multiple arrays. The no. of arrays resulted would keep changing depending upon the no. of tags in array a[].
As a first step, my query and result I get is as shown below:
let a=["Men","Women","Accessories"]
let c=(for i in a
Let d=Concat("Tags/",i)
return d)
for i in c
let m=(for y in outbound i TC
return y._key)
return m
and result I get is:
[
[
"C1",
"C5",
"C7",
"C3"
],
[
"C2",
"C5",
"C6",
"C4"
],
[
"C7",
"C5",
"C6"
]
]
From this result, I want only common element as a result i.e "C5" (here).
How can I get that?
This question has also been asked and answered on github.
The function INTERSECTION() returns the intersection of all specified arrays and APPLY() is used to pass a dynamic amount of nested arrays.
The query
let D = [["C1","C5","C7","C3"],["C2","C5","C6","C4"],["C7","C5","C6"]]
RETURN APPLY("INTERSECTION", D)
results in:
[
[
"C5"
]
]

Lodash union of arrays of mongoose ObjectId

I have 2 arrays that containing ObjectId items: array1 array2
I want to create a union between those 2 arrays. for that i'm running:
let res = _.union(array1, array2);
But res contains duplicates ObjectId.
How can i solve this?
If you're looking at two different arrays with strings of 'objectIDs' you can use concat and then uniq to remove the duplicates. Don't forget to run valueOf at the end of your Lodash chain to call it to execute.
Below is an illustrative example:
let array1 = ['42142141221421d', '9999'];
let array2 = ['s421421412412fef3', '42142141221421d', '1234'];
const res = _(array1)
.concat(array2)
.uniq()
.valueOf();
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
You could try using _.uniqBy(res, 'ObjectId'); That should remove duplicate objectId in your new res array
let newRes = _.uniqBy(res, 'ObjectId');

Groovy: How to sort String array of text+numbers by last digit

I have array like this:
def arr = [
"v3.1.20161004.0",
"v3.1.20161004.1",
"v3.1.20161004.10",
"v3.1.20161004.11",
"v3.1.20161004.2",
"v3.1.20161004.3",
"v3.1.20161004.30",
]
I need to get this:
def arr = [
"v3.1.20161004.0",
"v3.1.20161004.1",
"v3.1.20161004.2",
"v3.1.20161004.3",
"v3.1.20161004.10",
"v3.1.20161004.11",
"v3.1.20161004.30",
]
How to sort it by last number '.x' ?
You can tokenize each string on . and then grab the last element as an Integer, and sort on this (passing false to return a new list)
def newArray = arr.sort(false) { it.tokenize('.')[-1] as Integer }
When sorting an array you can define a sorting closure. In this case you can split on the dot and sort using the spaceship operator:
arr.sort { a, b -> a.tokenize('.').last().toInteger() <=> b.tokenize('.').last().toInteger() }

Resources