Dynamic array keys
This is kind of non-trivial because you want to nest, but it should go something like:
function insert_using_keys($arr, $keys, $value){
// we're modifying a copy of $arr, but here
// we obtain a reference to it. we move the
// reference in order to set the values.
$a = &$arr;
while( count($keys) > 0 ){
// get next first key
$k = array_shift($keys);
// if $a isn't an array already, make it one
if(!is_array($a)){
$a = array();
}
// move the reference deeper
$a = &$a[$k];
}
$a = $value;
// return a copy of $arr with the value set
return $arr;
}
Dynamically creating keys in a JavaScript associative array
Use the first example. If the key doesn't exist it will be added.
var a = new Array();
a['name'] = 'oscar';
alert(a['name']);
Will pop up a message box containing 'oscar'.
Try:
var text = 'name = oscar'
var dict = new Array()
var keyValuePair = text.replace(/ /g,'').split('=');
dict[ keyValuePair[0] ] = keyValuePair[1];
alert( dict[keyValuePair[0]] );
Sum array of objects with dynamic keys - Javascript
Here's my solution. Map through the array and flatten the properties values, then reduce them.
const arr = [
{ 'Atencion Personalizada': 2, 'Caja': 3 },
{ 'Atencion Personalizada': 1 },
{ 'Tarifa Social': 3 }
]
console.log(arr.flatMap(e => Object.values(e)).reduce((a, b) => a + b));
Create Array by Dynamic Keys and Values
let name = 'Vishesh';
let name2 = 'Vishesh2';
let cars = [{carName: "Mercedes"},{carName: "Volvo"},{carName:"BMW"}];
let array = []
array.push({[name]:cars})
array.push({[name2]:cars})
console.log(array);
loop through a dynamic key of an object in a array
Just use the Bracket notation:
additionalFields.map((opts) => {
if(fields[opts].length > 0){ //this one doesn't work...
//do something
}
}
Example:
const person = {
firstname: 'John',
lastname: 'Doe'
}
const key = "firstname"
console.log(person[key]) // John
Dynamic keys in Javascript associative array declaration one-liner
It is now possible to use dynamic keys in the declaration of a javascript object, in any browser/platform that supports ES6 literal shorthands:
key = "dynamic";
arr2 = {
[key]: "foo", // "dynamic": "foo"
"bar": "baz"
};
Reduce Array of Objects with Dynamic Keys
Get the key/value pairs and update the sum.
var data = [{ A: 4, B: 2 }, { A: 2, B: 1 }, { A: 3, B: 1 }, { A: 2, B: 1, C: 1 }], result = data.reduce((r, o) => (Object.entries(o).forEach(([k, v]) => r[k] = (r[k] || 0) + v), r), {});
console.log(result);
TypeScript array.map() with dynamic keys
You can probably do this by creating a new object in the map and returning it?
Something like
const fooArrayWithLimitedKeys = fooArray.map(item => arrayOfKeys.reduce(
(accumulator, key) => {
accumulator[key] = item[key]
return accumulator
}, {})
)
it can also be written without reduce
like follows:
const fooArrayWithLimitedKeys = fooArray.map(item => {
const returnValue = {}
arrayOfKeys.forEach(key => {
returnValue[key] = item[key]
})
return returnValue;
})
KQL - Convert Dynamic Array of Key Value to Key Value dictionary
you can use a combination of mv-apply
and make_bag()
:
print d = dynamic([
{"key": "value"},
{"ProjectId": "1234"},
{"ProjectName": "Albatros"},
{"User": "Bond"}
])
| mv-apply d on (
summarize result = make_bag(d)
)
result |
---|
{ "key": "value", "ProjectId": "1234", "ProjectName": "Albatros", "User": "Bond" } |
How to access the dynamic key in an array of objects
you can do it with a simple for...in
statement like this:
for (var i = 0; i <= trackobj.trackees.length; i++) {
for(var key in trackobj.trackees[i]) {
console.log(key);
}
}
Related Topics
Get Current Url Path with Query String in PHP
Why Does 1234 == '1234 Test' Evaluate to True
How to Declare a Two Dimensional Array
How to Convert Ipv6 from Binary for Storage in MySQL
Using Str_Split on a Utf-8 Encoded String
What Is the Format for E-Mail Headers That Display a Name Rather Than the E-Mail
Accessing Session from Twig Template
Get the Last Modified Date of a Remote File
How to Fix "Set Samesite Cookie to None" Warning
PHP - Import CSV File to MySQL Database Using Load Data Infile
How to Access an Application Parameters from a Service
Get the First Letter of Each Word in a String
Should My PHP Functions Accept an Array of Arguments or Should I Explicitly Request Arguments
Laravel 5 Class 'Form' Not Found