I want to create ,multidimensional array in swift with multiple inner arrays - arrays

I want to create ,multidimensional array in swift with multiple inner arrays. I want the structure look like this but getting error while creating this. Please guide me how can I create this structure.
class ViewController: UIViewController
{
var data2:[String] = []
var LocationPickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue"]
var LocationPickerData2 = ["Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var LocationPickerData3 = ["Goat Cheese", "Asiago"]
var Title = ["twwwitle1","sdfsaf","Prsdfasovosdfasflone","sfsa","sdfsf Blue"]
var Title2 = ["seeefdasf Cheddar","sdfsf Jack","fsaf","Gouda","fasf Cheese", "sdfsf"]
var Title3 = ["ddddsdfs ff", "fasfsdffasf"]
var Name = ["fsdfa","fasfsafsf","Provolone","Brie","Maytag Blue"]
var Name2 = ["afsadff Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var Name3 = ["fffffff ffff", "jjjjjjj"]
var data2 = [
location[
location1[ LocationPickerData ],
location2[ LocationPickerData2],
location3[ LocationPickerData3]
],
titles[
title1[Title],
title2[Title2],
title3[Title3]
],
names[
name1[Name],
name2[Name2],
name3[Name3]
]
]
}

Declare your multidimensional array like this:
var data2 = [
[
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
[
Title
Title2,
Title3,
],
[
Name
Name2,
Name3,
]
]
Or you can declare a dictionary:
var data2 = [
"Locations": [
LocationPickerData
LocationPickerData2,
LocationPickerData3,
],
"Titles": [
Title
Title2,
Title3,
],
"Names": [
Name
Name2,
Name3,
]
]

Related

Adding key value pair in dictionary swift

Given this
var applicationPayload =
[
"main": [
"key1": "value1",
"key2": [
"inside1": "insideValue1"
]
]
]
How can I append or add something inside key2 property.. I'd like the result to be something like this
[
"main": [
"key1": "value1",
"key2": [
"newInside2": "newInsideValue2",
"inside1": "insideValue1"
]
]
]
I tried doing this does not seems to work
applicationPayload["main"]["key2"]["newInside2"] = "newInsideValue2"
You could create a variable containing the current value of key2 and modify that before rewriting it back into the original dictionary:
import Foundation
var applicationPayload =
[
"main": [
"key1": "value1",
"key2": [
"inside1": "insideValue1"
]
]
]
print("Before: \(applicationPayload)")
var key2Value = applicationPayload["main"]?["key2"] as! [String: String]
key2Value["newInside2"] = "newInsideValue2"
applicationPayload["main"]?["key2"] = key2Value
print("After: \(applicationPayload)")
Output:
Before: ["main": ["key1": "value1", "key2": ["inside1": "insideValue1"]]]
After: ["main": ["key1": "value1", "key2": ["inside1": "insideValue1", "newInside2": "newInsideValue2"]]]
You can try for easy access/assign with keyPath
// get value of key2
var res = applicationPayload[keyPath:"main.key2"] as! [String:Any]
// add the other key
res["newInside2"] = "newInsideValue2"
// mutate key2
applicationPayload[keyPath:"main.key2"] = res
For more about keypaths check https://oleb.net/blog/2017/01/dictionary-key-paths/

filter the records using loadash angularjs

var arr = Array [ Object, Object ]
into arr {
"name" : "xyz",
"age" : "22",
"sports" : Array[
{
id:1,
name : "xyz",
},
{
id:2,
name : "yyy",
},
]
}
var obj = Object { name: "JXZ", id :1};
var response = _.filter(arr, function(user) {
return user.sports.id === obj.id;
});
filter the record that match with obj's id from arr's sports.id return array object.
I'm suppose to put loop but i guess that not right solution if please
Please try this below code.
<script>
var app = angular.module('app',[]);
app.controller('ctrl',function($scope){
$scope.arr = Array [ Object, Object ];
$scope.arr = {
"name" : "xyz",
"age" : "22",
"sports" : [
{
id:1,
name : "xyz",
},
{
id:2,
name : "yyy",
}
]
}
var obj = { name: "JXZ", id :1};
var filteredSports = _.filter($scope.arr.sports, function (sport) {
return sport.id == obj.id;
})
$scope.arr.sports = filteredSports;
console.log($scope.arr);
})
</script>

Loop through Dictionary in Swift

I have a dictionary which I want to append some items.
**This is my dictionary which I want to achieve: **
let parameters: [String: Any] = [
{
"ResultsList": [{
"UserId": "b806e283-066f-4081-aafe-1fe216a57c35",
"FriendUserId": "7a2ec150-cdb3-4600-84f8-2dab970bfa0c",
"TransferDate": "2017-11-23",
"UserAnswers": [{
"AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
"LastAnsweredDate": "2017-11-23",
"QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
},
{
"AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
"LastAnsweredDate": "2017-11-23",
"QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
}
]
}]
}
]
And this is my current dictionary which I want to make the loop and add the items.
let parameters: [String: Any] = [
{
ResultsList: [
{
UserId: “b806e283-066f-4081-aafe-1fe216a57c35”
FriendUserId: “7a2ec150-cdb3-4600-84f8-2dab970bfa0c”
TransferDate: “2017-11-23”
UserAnswers: [
{
AnswerId: “b7562603-614d-11e7-a7e0-484d7ee0cd26"
LastAnsweredDate: “2017-11-23”
QuestionId : “0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
}
]
}
]
}
]
I have 3 arrays which I want to loop through and append to the dictionary
var AnswerId = [ “b7562603-614d-11e7-a7e0-484d7ee0cd26", “aasdaas-614d-11e7-a7e0-484d7ee0cd26", “b756asd03-614d-11e7-a7e0-484d7ee0cd26"]
var LastAnsweredDate = [“2017-11-23”, “2017-11-23”, “2017-11-22”]
var QuestionId = [“0b60f35e-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26"]
Can someone please help to achieve this result?

JSON how to get squared brackets?

I have the following code:
JSONArray array = new JSONArray();
array.put(allWaitClasses.get(0).allPairs.get(0).pair);
array.put(allWaitClasses.get(0).allPairs.get(1).pair);
array.put(allWaitClasses.get(0).allPairs.get(2).pair);
array.put(allWaitClasses.get(0).allPairs.get(3).pair);
array.put(allWaitClasses.get(0).allPairs.get(4).pair);
array.put(allWaitClasses.get(0).allPairs.get(5).pair);
json = array.toString();
What I get is the following:
[
{
"name": "User I/O"
},
{
"key": "61410583140000"
},
...
]
But what I want is squared brackets:
var data =[
[
1229904000000,
12.74
],
[
1229990400000,
115.20
],
...
Actually I want to follow the template of this API:
https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?
So how can I get the squared brackets insteand of the curved ones?
you should iterate over data and push items in following way :
var arrayX = [];
$.forEach( data, function ( item ) {
arrayX .push( [
item.timestamp, item.value
] );
} )

Format JSON Array format to another JSON Array format

Here is the my JSON Array format
[
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
The "1234","4567" and "7890" are my Ids and "Name11","Name12", "Name13", "Name21", "Name22" and "Name23" are the values for each corresponding Ids.
The JSON array contains two records as shown above. Now i need to convert the following JSON Format....
[
{
"1234":"Name11",
"4567":"Name12",
"7890":"Name13"
},
{
"1234":"Name21",
"4567":"Name22",
"7890":"Name23"
}
]
Please help me out, how can i construct the above mentioned JSON array format.
With pure javascript:
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
var newObj = []
for(var i = 0; i< src.length; i++){
var item = {}
for(var j = 0; j<src[i].length; j++){
if(j%2 === 0){
item[src[i][j]] = ""
}else{
item[src[i][j-1]] = src[i][j][0]
}
}
newObj.push(item)
}
You can do it with the following code (see the snippet below):
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
];
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
document.getElementById("res").textContent = JSON.stringify(res);
<div id="res"></div>

Resources