Get all keys with values for a specific date in an array of dictionaries SWIFT
I have an array of dictionaries:
var eventDetailsArray = [[String: String]]()
var events: [String: String] = ["name":"block A”, "date":"Friday, 23 Nov 2018"]
var events1: [String: String] = ["name":"block AB”, "date":"Friday, 23 Nov 2018"]
var events2: [String: String] = ["name":"block B", "date":"Tuesday, 13 Nov 2018"]
var events3: [String: String] = ["name":"block C", "date":"Wednesday, 28 Nov 2018"]
eventDetailsArray.append(events)
eventDetailsArray.append(events1)
eventDetailsArray.append(events2)
eventDetailsArray.append(events3)
var eventNamesArray = [String]()
I want an output array with event names for date selected only.
E.g; if ”Friday, 23 Nov 2018" is selected; I should get "block A” and "block AB” in the output array in SWIFT
ios swift
|
show 2 more comments
I have an array of dictionaries:
var eventDetailsArray = [[String: String]]()
var events: [String: String] = ["name":"block A”, "date":"Friday, 23 Nov 2018"]
var events1: [String: String] = ["name":"block AB”, "date":"Friday, 23 Nov 2018"]
var events2: [String: String] = ["name":"block B", "date":"Tuesday, 13 Nov 2018"]
var events3: [String: String] = ["name":"block C", "date":"Wednesday, 28 Nov 2018"]
eventDetailsArray.append(events)
eventDetailsArray.append(events1)
eventDetailsArray.append(events2)
eventDetailsArray.append(events3)
var eventNamesArray = [String]()
I want an output array with event names for date selected only.
E.g; if ”Friday, 23 Nov 2018" is selected; I should get "block A” and "block AB” in the output array in SWIFT
ios swift
You can usefilter()
for that. Did you try anything?
– Larme
Nov 15 '18 at 9:14
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method likefilter()
(which is valid too).
– Larme
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
You are encouraged to use a custom struct or class andDate
instances for the dates. One reason is that this string representation of a date is not sortable
– vadian
Nov 15 '18 at 9:25
|
show 2 more comments
I have an array of dictionaries:
var eventDetailsArray = [[String: String]]()
var events: [String: String] = ["name":"block A”, "date":"Friday, 23 Nov 2018"]
var events1: [String: String] = ["name":"block AB”, "date":"Friday, 23 Nov 2018"]
var events2: [String: String] = ["name":"block B", "date":"Tuesday, 13 Nov 2018"]
var events3: [String: String] = ["name":"block C", "date":"Wednesday, 28 Nov 2018"]
eventDetailsArray.append(events)
eventDetailsArray.append(events1)
eventDetailsArray.append(events2)
eventDetailsArray.append(events3)
var eventNamesArray = [String]()
I want an output array with event names for date selected only.
E.g; if ”Friday, 23 Nov 2018" is selected; I should get "block A” and "block AB” in the output array in SWIFT
ios swift
I have an array of dictionaries:
var eventDetailsArray = [[String: String]]()
var events: [String: String] = ["name":"block A”, "date":"Friday, 23 Nov 2018"]
var events1: [String: String] = ["name":"block AB”, "date":"Friday, 23 Nov 2018"]
var events2: [String: String] = ["name":"block B", "date":"Tuesday, 13 Nov 2018"]
var events3: [String: String] = ["name":"block C", "date":"Wednesday, 28 Nov 2018"]
eventDetailsArray.append(events)
eventDetailsArray.append(events1)
eventDetailsArray.append(events2)
eventDetailsArray.append(events3)
var eventNamesArray = [String]()
I want an output array with event names for date selected only.
E.g; if ”Friday, 23 Nov 2018" is selected; I should get "block A” and "block AB” in the output array in SWIFT
ios swift
ios swift
edited Nov 15 '18 at 9:12
ielyamani
7,94962761
7,94962761
asked Nov 15 '18 at 9:11
Saad RiazSaad Riaz
346
346
You can usefilter()
for that. Did you try anything?
– Larme
Nov 15 '18 at 9:14
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method likefilter()
(which is valid too).
– Larme
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
You are encouraged to use a custom struct or class andDate
instances for the dates. One reason is that this string representation of a date is not sortable
– vadian
Nov 15 '18 at 9:25
|
show 2 more comments
You can usefilter()
for that. Did you try anything?
– Larme
Nov 15 '18 at 9:14
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method likefilter()
(which is valid too).
– Larme
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
You are encouraged to use a custom struct or class andDate
instances for the dates. One reason is that this string representation of a date is not sortable
– vadian
Nov 15 '18 at 9:25
You can use
filter()
for that. Did you try anything?– Larme
Nov 15 '18 at 9:14
You can use
filter()
for that. Did you try anything?– Larme
Nov 15 '18 at 9:14
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method like
filter()
(which is valid too).– Larme
Nov 15 '18 at 9:17
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method like
filter()
(which is valid too).– Larme
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
You are encouraged to use a custom struct or class and
Date
instances for the dates. One reason is that this string representation of a date is not sortable– vadian
Nov 15 '18 at 9:25
You are encouraged to use a custom struct or class and
Date
instances for the dates. One reason is that this string representation of a date is not sortable– vadian
Nov 15 '18 at 9:25
|
show 2 more comments
2 Answers
2
active
oldest
votes
You can do it like so:
let str = "Friday, 23 Nov 2018"
eventNamesArray = eventDetailsArray.compactMap { $0["date"] == str ? $0["name"] : nil }
And the result is:
print(eventNamesArray) //["block A", "block AB"]
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
add a comment |
Just use the filter
method:
var eventNamesArray = eventDetailsArray.filter { $0["date"] == "Friday, 23 Nov 2018" }
Maybe you should also consider using Date
instead of Strings to represent the date. Especially, if you are interested in supporting different languages / locales.
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315897%2fget-all-keys-with-values-for-a-specific-date-in-an-array-of-dictionaries-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do it like so:
let str = "Friday, 23 Nov 2018"
eventNamesArray = eventDetailsArray.compactMap { $0["date"] == str ? $0["name"] : nil }
And the result is:
print(eventNamesArray) //["block A", "block AB"]
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
add a comment |
You can do it like so:
let str = "Friday, 23 Nov 2018"
eventNamesArray = eventDetailsArray.compactMap { $0["date"] == str ? $0["name"] : nil }
And the result is:
print(eventNamesArray) //["block A", "block AB"]
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
add a comment |
You can do it like so:
let str = "Friday, 23 Nov 2018"
eventNamesArray = eventDetailsArray.compactMap { $0["date"] == str ? $0["name"] : nil }
And the result is:
print(eventNamesArray) //["block A", "block AB"]
You can do it like so:
let str = "Friday, 23 Nov 2018"
eventNamesArray = eventDetailsArray.compactMap { $0["date"] == str ? $0["name"] : nil }
And the result is:
print(eventNamesArray) //["block A", "block AB"]
answered Nov 15 '18 at 9:23
ielyamaniielyamani
7,94962761
7,94962761
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
add a comment |
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
1
1
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
did exactly as I wanted. thanks :)
– Saad Riaz
Nov 15 '18 at 9:33
add a comment |
Just use the filter
method:
var eventNamesArray = eventDetailsArray.filter { $0["date"] == "Friday, 23 Nov 2018" }
Maybe you should also consider using Date
instead of Strings to represent the date. Especially, if you are interested in supporting different languages / locales.
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
add a comment |
Just use the filter
method:
var eventNamesArray = eventDetailsArray.filter { $0["date"] == "Friday, 23 Nov 2018" }
Maybe you should also consider using Date
instead of Strings to represent the date. Especially, if you are interested in supporting different languages / locales.
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
add a comment |
Just use the filter
method:
var eventNamesArray = eventDetailsArray.filter { $0["date"] == "Friday, 23 Nov 2018" }
Maybe you should also consider using Date
instead of Strings to represent the date. Especially, if you are interested in supporting different languages / locales.
Just use the filter
method:
var eventNamesArray = eventDetailsArray.filter { $0["date"] == "Friday, 23 Nov 2018" }
Maybe you should also consider using Date
instead of Strings to represent the date. Especially, if you are interested in supporting different languages / locales.
answered Nov 15 '18 at 9:15
Andreas OetjenAndreas Oetjen
4,45611125
4,45611125
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
add a comment |
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
sure, thank you @Andreas
– Saad Riaz
Nov 15 '18 at 9:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315897%2fget-all-keys-with-values-for-a-specific-date-in-an-array-of-dictionaries-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can use
filter()
for that. Did you try anything?– Larme
Nov 15 '18 at 9:14
I am new to programming, However I tried for loop for that: for i in eventDetailsArray{ } but nothing fruitful
– Saad Riaz
Nov 15 '18 at 9:15
And what was your attempt? There is nothing wrong in doing a for loop, if you are new to programming. That's basic logic that might help you in the future instead of using a high level method like
filter()
(which is valid too).– Larme
Nov 15 '18 at 9:17
The given example doesn't compile. Please formulate a Minimal, Complete, and Verifiable Examples
– Rakesha Shastri
Nov 15 '18 at 9:17
You are encouraged to use a custom struct or class and
Date
instances for the dates. One reason is that this string representation of a date is not sortable– vadian
Nov 15 '18 at 9:25