objective c group array - objective-c

I have array like this:
{
toNumber = +79995840405;
type = 9;
}
{
toNumber = +79995840405;
type = 65;
}
{
toNumber = +79995840405;
type = 9;
}
{
toNumber = +79995840405;
type = 65;
}
How can I group items by toNumber & type? thanks

You have provided little detail, which makes it hard for people to help you; and haven't shown what you have tried yourself and explained where you got stuck, which is the SO approach - people here will help you, not do the work for you.
The above is why you are getting close votes.
That said let's see if we can point you in the right direction, but understand this is based on guesswork about what you have and your problem.
So it sounds like you have an array (NSArray) of dictionaries (NSDictionary) and wish to produce a dictionary of arrays. A straightforward iteration can be used for that:
Create an empty result dictionary (NSMutableDictionary)
Iterate over your array looking at each element (foreach)
Using the type value of your element as the key value of your result dictionary:
3.1. If there is no entry in your result dictionary for the key create a new array (NSMutableArray), add the element's toNumber value to it, and add the array to your result dictionary.
3.2 Otherwise simply add to toNumber value to the existing array at the key entry of your result dictionary.
That's it, each bullet is a line or two of code.
If you get stuck as a new question, providing details, showing your code, and explaining what you problem is. Someone will undoubtedly help you from there.
HTH

Related

Fetch Id of a selected data from a array of data

I have a list of array id but when I select any data I want the id of the selected data, I tried using map but it gives me a list of id's in a array. Please let me know how to do this, thanks in advance
Could you provide an example in order to understand your question better?
map() will always return an array, if you are getting an array with the desired value and a long list of undefined, then just add
.filter(e => e)
at the end.
If you want to iterate and obtain a single value, reduce() might be what you are looking for.

What's the meaning of 'i' in for loop?

In almost every programming language, they use variable i when they explain for loop. Like,
for i in 'string':
print(i)
or
for(var i ; i<100 ; i++) {
console.log(i);
}
etc...
Does it mean anything? Or just a variable with no meaning?
I couldn't find any information about this in google search.
You have given two different example.
For the first one
for i in 'string':
print(i)
For this one, the variable 'i' is the variable where the value will be put from your parameter (here 'string'). If i gave an array as parameter for instance, each value of array will be put in 'i', step by step (element [0], then element[1], etc...).
Note that this is a simplified view of this question and it doesn't work exactly like that for the program. But you can understand it as it.
For the second one
for(var i ; i<100 ; i++) {
console.log(i);
}
You declare explicitly a variable to be used. This declared will be used by the loop, incrementig by the step you had defined until it reaches limit you also defined.
It is slightly the same thing for both example. Hope i explain well
it is just a common practice. Many people when learning see i being used as a variable name. But that's all it is. It is just a name that could be anything, you could us any other sequence of characters!
i is just a variable. Generally, it is referred to as "iteration for the loop".

Access Javascript Array with numeric key

I tried to access the following value in an multidimensional array:
var temp = allSliderData[i]['slider_full'][j].path;
and it works fine, but the following not
var temp2 = allSliderData[i]['slider_thumbs'][j].285x255;
with the answer
"SyntaxError: identifier starts immediately after numeric literal"
I now tried escapings, array functions .. this message is good known in stackoverflow .. but still no luck.
Could anybody help out?
THANKS!!
Something like that won't work. You can't have a property named "2" there. What exactly do you want to do?

Golang server: send JSON with SQL query result that has variable number of columns

I'm creating a little implementation of RESTful API with Go server.
I'm extracting query parameters from URL (I know it's not safe, I'll try to fix this later on, but if you have any recommendations even on this topic, they would be helpful).
I have table name, desired columns and some conditions saved in 3 sring variables.
I'm using this query:
rows, _ := db.Query(fmt.Sprintf("SELECT %s FROM %s WHERE %s", columns, table, conditions))
I want to send the query result back to my frontend, as JSON. I have variable number of unknown columns, so I can't do it "standard" way.
One solution I can think of is to build a JSON string "manually" from from query result and rows.Columns().
But I'd like to do this in a more sofisticated way using something like variadic interface and stuff like that. The problem is, even after trying a lot, I still dont understand how it works.
I tried using following code
cols, err := rows.Columns() // Get the column names; remember to check err
vals := make([]sql.RawBytes, len(cols)) // Allocate enough values
ints := make([]interface{}, len(cols)) // Make a slice of []interface{}
for i := range ints {
vals[i] = &ints[i] // Copy references into the slice
}
for rows.Next() {
err := rows.Scan(vals...)
// Now you can check each element of vals for nil-ness,
// and you can use type introspection and type assertions
// to fetch the column into a typed variable.
}
from this tutorial but it doesn't work, I'm getting errors like
cannot use &ints[i] (type *interface {}) as type sql.RawBytes in assignment
And even if it'd work, I dont understand it.
Does anyone have a good solution for this? Some explanation would be great aswell.
Thanks a lot.
The first problem is here:
for i := range ints {
vals[i] = &ints[i] // Copy references into the slice
}
This is you setting values that are meant to be RawBytes as pointers to interfaces.
Before I explain what that's meant to be doing I'll see if I can explain what the general idea is here.
So normally when getting the response from SQL in Go you'd have a slice with each column and type (id int, name string, ...) so you can then read each SQL record into this slice and each column will be mapped to the value of the same type.
For cases like yours where you will have more variety in the response from SQL and need Go to handle it, you'd do this:
for i := range ints {
ints[i] = &vals[i] // Copy references into the slice
}
What this is saying is each of your interface values holds a pointer to the vals array that will hold the response from SQL. (In my examples I use [][]byte instead of RawBytes so value in vals would be a slice of byte values from SQL.)
You'd then do:
err := rows.Scan(ints...)
Since interface can evaluate to any type, when the ints array becomes populated it will accept any value then update the position in vals array based on the pointer with the value from SQL as a RawBytes type.
HTH

Newbie issue with LINQ in vb.net

Here is the single line from one of my functions to test if any objects in my array have a given property with a matching value
Return ((From tag In DataCache.Tags Where (tag.FldTag = strtagname) Select tag).Count = 1)
WHERE....
DataCache.Tags is an array of custom objects
strtagname = "brazil"
and brazil is definitely a tag name stored within one of the custom objects in the array.
However the function continually returns false.
Can someone confirm to me that the above should or should not work.
and if it wont work can someone tell me the best way to test if any of the objects in the array contain a property with a specific value.
I suppose in summary I am looking for the equivalent of a SQL EXISTS statement.
Many thanks in hope.
Your code is currently checking whether the count is exactly one.
The equivalent of EXISTS in LINQ is Any. You want something like:
Return DataCache.Tags.Any(Function(tag) tag.FldTag = strtagname)
(Miraculously it looks like that syntax may be about right... it looks like the docs examples...)
Many Thanks for the response.
Your code did not work. Then I realised that I was comparing to an array value so it would be case sensitive.
However glad I asked the question, as I found a better way than mine.
Many thanks again !