How can I turn a string into multiple sub arrays in Javascript? - arrays

I would like to turn this:
"a:1,b:2,c:3"
Into:
[['a', '1'],['b', '2'],['c', '3']]

Here is the same idea, but a little cooler ;)
let str = "a:1,b:2,c:3";
let result = str.split(',').map(x=>x.split(":"))
console.log(result);

You could use the JavaScript split method here and do it twice.
Something like the following:
let array = [];
let str = "a:1,b:2,c:3";
let splitStr = str.split(',');
splitStr.forEach(subStr => {
array.push(subStr.split(':'));
});
console.log(array);

Related

how can I combine 2 array to create array of arrays in swift

I have latsArr and LongsArr filled from firebase automatically.
I want to populate latsAndLongsArray in viewDidLoad function. How can I do that?
var latsArr = [1111.0,2222.0,333.0]
var longsArr = [444.0,555.0,666.0]
var latsAndLongs = [[111.0,444.0],[222.0,555.0],[333.0,666.0]]
Use the zip(_:_:) and map(_:) methods combined to get the expected result:
let latsAndLongs = zip(latsArr, longsArr).map { [$0.0, $0.1] }
var latsAndLongs = zip(latsArr, longsArr).map({[$0.0, $0.1]})
One option (which uses tuples instead of arrays) is to use zip.
var latsArr = [1111.0,2222.0,333.0]
var longsArr = [444.0,555.0,666.0]
var latsAndLongs = zip(latsArr, longsArr)
// latsAndLongs == [(1111.0, 444.0), (2222.0, 555.0), (333.0, 666.0)]

Using _.some in lodash

I have Typescript code where i want to return true if any items in 1 array are present in another array. I am new to lodash but i was looking to do this using _.some. I am not sure if this is the correct approach. The code below is returning false but i expected it to return true.
let array1 = ["test", "some", "lodash"];
let array2 = ["some", "includes"];
let condition : boolean = _.some(array1, array2);
You can use intersection function and check if it returns any items:
let condition : boolean = _.intersection(array1, array2).length > 0;
With some you have to pass a test callback as a second argument:
let condition : boolean = _.some(array1, item => array2.includes(item))
lodash was cool before plain javascript had the same methods...
let array1 = ["test", "some", "lodash"];
let array2 = ["some", "includes"];
let test = array1.some(e => array2.includes(e));
console.log(test);

Convert array of objects to Array Angularjs

Is there any possible way in angularJs to convert this array of objects:
[{"tickets":1,"month":"june","year":2016},{"tickets":2,"month":"june","year":2015},{"tickets":3,"month":"december","year":2015}]
to an array like this:
[['tickets', 'month','year'], [1, "june",2016],[3, "june",2015],[1, "december",2015]]
Approach using Array#reduce() and Array#concat() that doesn't rely on knowing any of the property names or hard coding resultant array structure
let data = [{"tickets":1,"month":"june","year":2016},{"tickets":2,"month":"june","year":2015},{"tickets":3,"month":"december","year":2015}];
let res = data.reduce((acc, curr) => {
return acc.concat([acc[0].map((key) => curr[key])]);
}, [Object.keys(data[0])]);
console.log(res)
Sure, its pure javascript can handle
var array1 = [{"tickets":1,"month":"june","year":2016},
{"tickets":2,"month":"june","year":2015},
{"tickets":3,"month":"december","year":2015}];
var array2 = [['tickets', 'month','year']];
array1.forEach(function(item){
array2.push([item.tickets, item.month, item.year]);
})
console.log(array2);
UPDATE
More flexible way, adviced by JK_Jha
var array1 = [{"tickets":1,"month":"june","year":2016},
{"tickets":2,"month":"june","year":2015},
{"tickets":3,"month":"december","year":2015}];
var array2 = [Object.keys(array1[0])];
array1.forEach(function(item){
array2.push([item.tickets, item.month, item.year]);
})
console.log(array2);

string convert into array on the behalf of comma in angularjs

I have string something like this "12,13" and I want to convert into array like this [12,13] in controller. I used split function it does not work.
$scope.mySplit = function(string, nb) {
var array = string.split(',');
return array[nb];
}
$scope.isChecked = function(id,matches) {
var isChecked = false;
var arr= [];
arr = $scope.mySplit(matches,0);
console.log(arr);
};
One problem might be that you did not define arr to be a variable as such: var arr = $scope.mySplit(matches,0);. Or another variable. Unless you declared it globally somewhere else.
Look at this jsfiddle that works: https://jsfiddle.net/wapp4u5g/

Swift: Get multiple array values like "x"

For example, I have an array like var myArray = ['player_static.png', 'player_run0.png', 'player_run1.png', 'player_run2.png', 'player_jump0.png', 'player_jump1.png']
Is there any simple way to get only the "player_runX.png" images?
You can use filter to get all elements that hasPrefix("player_run"):
let myArray = ["player_static.png", "player_run0.png", "player_run1.png", "player_run2.png", "player_jump0.png", "player_jump1.png"]
let playerRuns = myArray.filter{$0.hasPrefix("player_run")}
print(playerRuns) //["player_run0.png", "player_run1.png", "player_run2.png"]
One way to do this would be to iterate over the array and retrieve the elements that match the pattern. A very quick sample would be something like this:
var myArray = ["player_static.png", "player_run0.png", "player_run1.png", "player_run2.png", "player_jump0.png", "player_jump1.png"]
func getSubArray(array:[String],prefix:String) -> [String]
{
var newArray = [String]()
for img in array
{
if img.substringToIndex(img.startIndex.advancedBy(prefix.characters.count)) == prefix
{
newArray.append(img)
}
}
return newArray
}
var test = getSubArray(myArray, prefix: "player_run")

Resources