Get value of a key from a dictionary array JavaScript
This should help:
const car = [{id: "1", brand: "Opel"}, {id: "2", brand: "Haima"},{id: "3", brand: "Toyota"}];
const brands = car.map(({ brand }) => brand);
Find key-value pair in an array of dictionaries
The following works. You can iterate through the items of each array comparing them to the items in the other:
for item1 in arrayOne {
for item2 in arrayTwo {
if item1 == item2 {
println("found a dictionary common to both arrays")
}
}
}
Searching for common key/value pairs in the two arrays:
for dict1 in arrayOne {
for (key, value) in dict1 {
for dict2 in arrayTwo {
if dict2[key] == value {
println("found \(key):\(value) in both arrays")
}
}
}
}
And if you're searching for a particular key:
let key = "title"
for dict1 in arrayOne {
if let value = dict1[key] {
for dict2 in arrayTwo {
if dict2[key] == value {
println("found \(key):\(value) in both arrays")
}
}
}
}
You can write your own arrayContains
function that takes the types you need:
func arrayContains(array:[[String:String]], value:[String:String]) -> Bool {
for item in array {
if item == value {
return true
}
}
return false
}
And then use it to find the values you want:
// Save all of the dictionaries from the first array that aren't in the second array
var newArray:[[String:String]] = []
for item in arrayOne {
if !arrayContains(arrayTwo, item) {
newArray.append(item)
}
}
To search the array for a particular key/value pair:
func arrayContains(array:[[String:String]], #key: String, #value: String) -> Bool {
for dict in array {
if dict[key] == value {
return true
}
}
return false
}
let result = arrayContains(arrayTwo, key: "title", value: "Alien!")
In Python, how do I find keys in an array of dictionaries where the values are the same?
As suggested in other answers, create a master dictionary that groups each key, then check their uniqueness.
# all keys are the same, so get the list
keys = array_of_dicts[0].keys()
# collapse values into a single dictionary
value_dict = {k: set(d[k] for d in array_of_dicts) for k in keys}
# get list of all single-valued keys
print([k for k, v in value_dict.items() if len(v) == 1])
Get key values from array of dictionaries
In objective-c
NSMutableArray *c = [[NSMutableArray alloc] init];
for (NSNumber *i in a) {
BOOL iFound = NO;
for (NSDictionary *dict in b) {
if ((NSNumber *)[[dict allKeys] firstObject] == i) {
iFound = YES;
[c addObject:(NSString *)[[dict allValues] firstObject]];
}
}
if (!iFound) {
[c addObject: [NSNull null]];
}
}
NSLog(@"%@",c);//("<null>",a,b)
In swift
let a = [1,2,3]
let b = [[2:"a"],[3:"b"],[45:"r"],[16:"a"]]
let c = a.map { i in b.first(where: { $0.keys.first == i })?.values.first }
print(c)//[nil, Optional("a"), Optional("b")]
How to find length of dictionary values
Sure. In this case, you'd just do:
length_key = len(d['key']) # length of the list stored at `'key'` ...
It's hard to say why you actually want this, but, perhaps it would be useful to create another dict that maps the keys to the length of values:
length_dict = {key: len(value) for key, value in d.items()}
length_key = length_dict['key'] # length of the list stored at `'key'` ...
Get key by value in dictionary
There is none. dict
is not intended to be used this way.
dictionary = {'george': 16, 'amber': 19}
search_age = input("Provide age")
for name, age in dictionary.items(): # for name, age in dictionary.iteritems(): (for Python 2.x)
if age == search_age:
print(name)
Search by value of dict in array
Convert list
of dict
to dict
d = {x["token"]: x for x in large_list}
d["4kj13"]["value1"]
# 10
How to add values of a key without losing any value in dictionary python when merging two list as dictionary?
Try this:
name = ['A', 'A', 'B', 'B', 'C', 'D', 'D', 'D']
num = [10, 20, 30, 40, 50, 60, 70, 80]
dict = {}
for key, value in zip(name, num):
if key not in dict:
dict[key] = value
else:
dict[key] += value
print(dict)
Related Topics
Checking for an Empty Result (Php, Pdo, and MySQL)
Add Class="Active" to Active Page Using PHP
Use a Variable Within Heredoc in PHP
Why Do Timestamps Have a Limit to 2038
Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out
Make Text Wrap in a Cell with Fpdf
Adding Multiple Tabs to Woocommerce Single Product Pages
PHP Auto-Kill a Script If the Http Request Is Cancelled/Closed
How to Get Array Key from Corresponding Array Value
Apple Sign in "Invalid_Client", Signing Jwt for Authentication Using PHP and Openssl
Url Encode Equivalent in Ruby on Rails
How to Prevent PHP Files from Being Downloaded? and What Are Some Ways Someone Can Download Them
How Validate Unique Email Out of the User That Is Updating It in Laravel
What Does the Dot-Slash Do to PHP Include Calls
How to Use Http_X_Forwarded_For Properly