Lodash _.difference returns an empty array - arrays

Quick and simple question:
I have two arrays that look like this:
var arr1 = [10037, 8812, 2412]
var arr2 = [10037, 8813, 2405, 8815, 2407, 8812, 2412, 2412, 8815]
I use lodash difference() to generate a new variable:
var difference = _.difference(arr1, arr2)
I console log difference and i am expecting to see something like this:
[8813, 2405, 8815, 2407,2412,8815] but instead i get an empty array.
Based on the documentation, difference should give back a new array containing the difference beween the two, so why am i getting a new empty array here? what am i doing wrong?
Thanks for any explanation

_.difference returns values from the first array that are not present in any of the other arrays passed in.
All values in arr1 are also present in arr2, so the result is an empty array.
Documentation for _.difference is available here: https://lodash.com/docs/4.17.4#difference

Related

Using .map with a nested two-dimensional array

I have a nested array, like this:
aux = [["None", ""],["Average", "avg"],["Summation", "sum"],["Maximum", "max"],["Minimum", "min"],["Count", "count"],["Distinct Count", "distinctCount"],["Max Forever", "maxForever"],["Min Forever","minForever"],["Standard Deviation","stddev"]]
Now, what i want to do, is to append "1234" (it's an example) to the beginning of the second element of each array, but not on the original array, like this:
new_aux = aux.map {|k| [k[0],k[1].prepend("1234")]}
Problem is that with this, the original array is being changed. I was reading about this and the problem seems to be the manipulation of the string, because if i convert that element to symbol, for example, the original array its not changed, like i want, but i don't exactly get what is the problem here and how should i do this.
By doing this, in new_aux i get:
[["None", "1234"],
["Average", "1234avg"],
["Summation", "1234sum"],
["Maximum", "1234max"],
["Minimum", "1234min"],
["Count", "1234count"],
["Distinct Count", "1234distinctCount"],
["Max Forever", "1234maxForever"],
["Min Forever", "1234minForever"],
["Standard Deviation", "1234stddev"]]
Which is what i want, the thing is that i have the exact same thing in the original array, which is what i don't want.
prepend mutates a string itself, so using this method you change the source array. Use strings interpolation to achieve your goal new_aux = aux.map {|k| [k[0],"1234#{k[1]}"]}

How to show elements of array?

I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]

How to create a true new copy of an array in App Script for Google Sheet?

I am having the following problem with App Script in Google Sheet.
I want to make different copies of a row in my sheet base on a table. I want to do something like
input1=[[1,2,"a"]];
input2=[[4,5,"b"],[7,8,"c"]];
function (input1,input2) {
\\ input# is a row, ie. an array with single element, which is another array
\\ The rows input# represent are of equal length
out=[];
copy1=input1[0];//copy1 is a reference to input1[0]
copy2=input1[0];//copy2 is a reference to input1[0]
for (i=0;i<input1.length,i++) {//input1.length is 1
copy1[i]+=input2[0][i];
copy2[i]+=input2[1][i];
}
out.push(copy1,copy2);//copy1=[5,2,a] copy2=[8,2,a]
return out
}
I would expect out to look like
out=[[5,7,"ab"],[8,10,"ac"]];//[[5,2,a],[8,2,a]]
But it doesn't. The output looks like whenever I modified copy1 or copy2, it was input1 itself that was modified.
What is wrong here? How can I create a new array variable, assign its value as equal to an existing array and modify the new array without changing the old? Is it ok to have input arrays that whose elements (of elements) consist of mixed numeric and strings?
Using Slice() to return a copy of an Array
Try it this way:
function myFunction(input1,input2)
{
var input1=[[1,2,"a"]];
var input2=[[4,5,"b"],[7,8,"c"]];
var out=[];
var copy1=input1[0].slice();//slice returns a copy of the array
var copy2=input1[0].slice();
for (var i=0;i<input1[0].length;i++)//looping through all of elements of input1[0];
{
copy1[i]+=input2[0][i];
copy2[i]+=input2[1][i];
}
out.push(copy1,copy2);
Logger.log(out);//out=[[5,7,"ab"],[8,10,"ac"]];
}
For more information on slice look here.
This is a good question. I've struggled with it a few times myself.

create dynamic size array swift

I want to create an Array, if i make it like this it works:
var arrayEingabe = Array(count:30, repeatedValue:0)
If i make it like this it does not work:
var sizeArray = 30
var arrayEingabe = Array(count:sizeArray, repeatedValue:0)
At the end i want to change the size of my Array depending on what the user typed in.
I was searching the web for one hour now, but i could not find the answer.
Thanks for your help guys
Greets
Kove
Actually both your examples compiled OK for me, but you should be more specific about types. Something like:
var arrayCount:Int = 30
var arrayEingabe = Array(count:arrayCount, repeatedValue:Int())
actually this might be better for you:
var arrayEingabe = [Int]()
This creates an empty array, and as mentioned in the comments Swift arrays are mutable. You can add, replace and delete members as you want.
On Swift 3.0.2 :-
Use Array initializer method give below:-
override init(){
let array = Array(repeating:-1, count:6)
}
Here, repeating :- a default value for Array.
count :- array count.

Inserting data into an array sequentially

I am currently trying to figure out how to design some sort of loop to insert data into an array sequentially. I'm using Javascript in the Unity3D engine.
Basically, I want to store a bunch of coordinate locations in an array. Whenever the user clicks the screen, my script will grab the coordinate location. The problem is, I'm unsure about how to insert this into an array.
How would I check the array's index to make sure if array[0] is taken, then use array[1]? Maybe some sort of For loop or counter?
Thanks
To just add onto the end of an array, just use .push().
var myArray = [];
var coord1 = [12,59];
var coord2 = [87,23];
myArray.push(coord1);
myArray.push(coord2);
myArray, now contains two items (each which is an array of two coordinates).
Now, you wouldn't do it this way if you were just statically declaring everything as I've done here (you could just statically declare the whole array), but I just whipped up this sample to show you how push works to add an item onto the end of an array.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push for some reference doc on push.
In case you need to know the array's length when reading the array in the future, you can use the .length attribute.
var lengthOfArray = myArray.length;
Using the .push() method as suggested by jfriend00 is my recommendation too, but to answer your question about how to work out what the next index is you can use the array's length property. Because JavaScript arrays are zero-based The length property will return an integer one higher than the current highest index, so length will also be the index value to use if you want to add another item at the end:
anArray[anArray.length] = someValue; // add to end of array
To get the last element in the array you of course say anArray[anArray.length-1].
Note that for most purposes length will give the number of elements in the array, but I said "one higher than the current highest index" above because JavaScript arrays are quite happy for you to skip indexes:
var myArray = [];
myArray[0] = "something";
myArray[1] = "something else";
myArray[50] = "something else again";
alert(myArray.length); // gives '51'
// accessing any unused indexes will return undefined

Resources