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(',') }
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)
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"
]
]
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');
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() }