Insert character inside array data - node.js

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)

Related

Calculating the number of characters inside a list in python

I made a list with some character in it and I looped through it to calculate the number of a specific character and it returns the number of all the characters inside the list and not the one's that I said it to. Take a look at my code and if someone can help I will appreciate it!
This is the code:
array = ['a', 'b', 'c', 'a']
sum_of_as = 0
for i in array:
if str('a') in array:
sum_of_as += 1
print(f'''The number of a's in this array are {sum_of_as}''')
If you know the list is only ever going to contain single letter strings, as per your example, or if you are searching for a word in a list of words, then you can simply use
list_of_strings = ["a", "b,", "c", "d", "a"]
list_of_strings.count("a")
Be aware though that will not count things such us
l = ["ba", "a", "c"] where the response would be 1 as opposed to 2 when searching for a.
The below examples do account for this, so it really does depend on your data and use case.
list_of_strings = ["a", "b,", "c", "d", "ba"]
count = sum(string.count("a") for string in list_of_strings)
print(count)
>>> 2
The above iterates each element of the list and totals up (sums) the amount of times the letter "a" is found, using str.count()
str.count() is a method that returns the number of how many times the string you supply is found in that string you call the method on.
This is the equivalent of doing
count = 0
list_of_strings = ["a", "b,", "c", "d", "ba"]
for string in list_of_strings:
count += string.count("b")
print(count)
name = "david"
print(name.count("d"))
>>> 2
The if str('a') in array evaluates to True in every for-loop iteration, because there is 'a' in the array.
Try to change the code to if i == "a":
array = ["a", "b", "c", "a"]
sum_of_as = 0
for i in array:
if i == "a":
sum_of_as += 1
print(sum_of_as)
Prints:
2
OR:
Use list.count:
print(array.count("a"))

Nodejs - return a subset of a list such that they contain values in the other list

So suppose there are two lists
var parsedList = ['a','b','c','d']
var originalList = ['a#','c#','h#']
And I would like to return a subset of originalList which contains values in the parsedList. (e.g. ['a#','c#'] as 'a' and 'c' is in the parsedList) Is there a simple and elegant way to do this?
This approach is very straightforward and elegant, first you loop over your originalList
and then for each element in that array, you want to check if parsedList includes it or not, and you can do that with parseList.includes()
var parsedList = ['a','b','c','d']
var originalList = ['a','c','h']
let newArr = []
originalList.forEach(element => {
if (parsedList.includes(element)){
newArr.push(element)
}
})
console.log(newArr) // ['a', 'c']
do you want something like that or you want to editoriginalList ?
in case you want it in the same array
originalList = originalList.filter(element => {
return parsedList.includes(element)
})
console.log(originalList) // ['a', 'c']

Merge elements from different lists in terraform 0.12

I am trying to create a list of maps from two different lists in terraform 0.12
E.g.
List1: ["a", "b", "c"]
List2: ["aa", "bb", "cc"]
Output required:
[{
"list1element" = "a"
"list2element" = "aa"
}, {
"list1element" = "b"
"list2element" = "bb"
}, {
"list1element" = "c"
"list2element" = "cc"
}]
If I could get the index of the element in the loop this would be so easy. Nested loops also make no sense.
If you know that the two lists will always have the same length, you can use the indices from one list with the other list:
[for i, v in list1 : {
list1element = list1[i]
list2element = list2[i]
}]

Puppet in cycle added empty elements in array

$hash_arr_1 = { b => 2, c => 3, f => 1 }
$arr = ['a', 'c', 'd', 'f', 'e']
$hash_arr_2 = $arr.map |$param| {
if has_key($hash_arr_1, $param) {
{$param => $hash_arr_1[$param]}
}
}
notice($hash_arr_2)
Result: [{ , c => 3, , f => 1, ,}]
How to do that there are no empty elements in the array?
The problem here is that you are using the map lambda function when really you want to be using filter. Summary from linked documentation is as follows:
Applies a lambda to every value in a data structure and returns an array or hash containing any elements for which the lambda evaluates to true.
So the solution for you is:
$hash_arr_2 = $hash_arr_1.filter |$key, $value| { $key in $arr }
This will iterate through the keys of the hash $hash_arr_1, check if the key exists as a member of the array $arr with the provided conditional, and then return a hash with only the key value pairs that evaluated to true.

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');

Resources