How do you tap the key of this object? - javascript

I'm working on a project for myself (gotta keep busy). I have JavaScript that taps API, and retrieves data as follows:
{
'2020-12-18:95': {
'45.0': [ [Object] ],
'50.0': [ [Object] ],
'55.0': [ [Object] ],
'60.0': [ [Object] ]
}
}
How do I enumerate through that? When I
object.2020-12-18:95
to get to the strike prices, I get error mesg. Your help is appreciated 😀

You can try using Object.keys() and .map() to iterate through as:
const data = {
'2020-12-18:95': {
'45.0': [ {} ],
'50.0': [ {} ],
'55.0': [ {} ],
'60.0': [ {} ]
}
}
const result = data['2020-12-18:95']
Object.keys(result)
.map(key => console.log(key, result[key]))
From the documentations:
The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Related

Convert a simple array to a two dimensional array of objects

I want to convert this:
[null, 1890, null, NGU]
...into this:
[[], [1890], [], [NGU]]
I've tried creating a new array and pushing values to it, but that just ends up looking the same. Honestly, I'm unsure of what to even call what I'm trying to create. Is it a two-dimensional array or an array of objects?
This is for a google app script and the documentation calls it a two-dimensional array of values.
var arr = [null, 1890, null, 'NGU']
var arr2d = arr.map(x => [x])
console.log(arr2d) // output --> [ [ null ], [ 1890 ], [ null ], [ 'NGU' ] ]

How to get a specific array from a list of arrays

I want to get a specific array based on a code, the array is something like this:
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
How to I get the array with code "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"?
I was using reduce to get the more recent image, but now I have no idea!
Array#find returns the first elmenent of an array that returns true for the given function.
const specificArray = arr.find(subArray => {
return subArray[2] === "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00";
}
Array#find (with destructuring) is best suited for this purpose.
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
const arr = [
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
1595619171842,
"0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
1595619171844,
"2f44a130-71d9-47ce-b5d5-04587c3c81fc"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
1595619171845,
"d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
],
[
"https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
1595619171846,
"940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
]
];
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
console.log(res);

adding json key names to a json values and changing key names

I have a json string that is in the format:
[
{
clientIDs:
"WELL #6",
analyteIDs:
[
"7440-62-2",
"7440-28-0"
]
}
]
I need to convert this to:
[
{
header:
"WELL #6",
items:
[
header: "7440-62-2",
header: "7440-28-0"
]
}
]
The values without a key name are throwing me off.
Unfortunately js cannot store a key value arrays, instead you have to use an object storing key and value. So the closes result you can achieve is following:
[
{
header:
"WELL #6",
items:
[
{ header: "7440-62-2" },
{ header: "7440-28-0" }
]
}
]
For that your steps will be following:
Assuming you have an array of objects.
Assuming the keys you want to change are static and will always exist in the objects
const myObjects = [
{
clientIDs:
"WELL #6",
analyteIDs:
[
"7440-62-2",
"7440-28-0"
]
}
]
myObjects.map((myObj) => {
myObj['header'] = myObj.clientIDs;
myObj['items'] = myObj.analyteIDs.map((item) => {
return { header: item }
});
// Keep in mind, if keys are dynamic and does not exist in some objects then this will fail
delete myObj['clientIDs'];
delete myObj['analyteIDs'];
});
console.log(myObjects);

How to map an array to another array and reset its keys in Javascript?

I have an object that has unique keys and each key holds an object:
var object = { 'a': {
source: '5279edf0-cd7f-11e3-af07-59475a41e2e9',
target: 'f6b3faa1-ad86-11e3-9409-3dbc47429e9f',
id: [ 'bf504d02-81e2-4a92-9c5c-8101943dc36d' ],
edge_context: [ 'small' ],
statement_id: [ '09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb' ],
weight: 2
},
'b': {
source: '5279edf1-cd7f-11e3-af07-59475a41e2e9',
target: 'f6b3faa1-ad86-11e3-9409-3dbc47429e9f',
id: [ 'de769846-9145-40f8-ab2d-91c0d9b82b27',
'd5723929-71a0-4dfe-bf03-94d43e358145' ],
edge_context: [ 'small' ],
statement_id:
[ '09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb',
'62671510-20ab-11e9-8cbf-ef11fdb08712' ],
weight: 6
}
}
var newArray = [];
for (let item of object) {
newArray(item);
}
console.log(newArray);
I want to map it to another array where the keys will be in a sequence 0, 1, 2 etc as the usual array
I tried to use this function above but it's not working saying "object is not iterable" so how to iterate the object?
Maybe:
const mappedObject = Object.keys(object).map(
k => object[k]
)
As others have pointed out, change the structure. It could be in the following way (you will obtain an array of objects, which you will be able to access using indexes like 0, 1, 2, etc):
var objt = [
{"a": {
"source": "5279edf0-cd7f-11e3-af07-59475a41e2e9",
"target": "f6b3faa1-ad86-11e3-9409-3dbc47429e9f",
"id": [ "bf504d02-81e2-4a92-9c5c-8101943dc36d" ],
"edge_context": [ "small" ],
"statement_id": [ "09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb" ],
"weight": 2
}
},
{"b": {
"source": "5279edf1-cd7f-11e3-af07-59475a41e2e9",
"target": "f6b3faa1-ad86-11e3-9409-3dbc47429e9f",
"id": [ "de769846-9145-40f8-ab2d-91c0d9b82b27",
"d5723929-71a0-4dfe-bf03-94d43e358145" ],
"edge_context": [ "small" ],
"statement_id":
[ "09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb",
"62671510-20ab-11e9-8cbf-ef11fdb08712" ],
"weight": 6
}
}
];
var newArray = objt.map(element => {
const firstProperty = Object.keys(element)[0];
let objectInfo = element[firstProperty];
console.log(objectInfo);
return objectInfo;
});
console.log(newArray);
What happens here is, the only field of each object is not named the same (in one object is "a", the next one is "b", and so on) so we need to figure out to get the only property of each object in the initial array, which contains the information you need to put in another array. For doing this. Object.keys() returns you an array of the properties of an object. Considering the scenario in which we only have one property per object, we get it using Object.keys(element)[0].
Finally, we just use .map() to generate a new array.
I would use Object.values(object) but it's not supported by IE (there is a polyfill for that). Or would use Object.getOwnPropertyNames (which is supported by IE) to convert the keys to an array and map the array to another which containing the values.
var newArray = Object.getOwnPropertyNames(object).map(key => object[key])

for of loop is only run once

I need to traverse a pretty deep object and I need to extract 12 values in different depths. My plan was to extract with the for of the values for each depth but I have a problem in the first depth.
I am a little confused about the for of behavior.
I thought this:
for (const key of Object.keys(jsonData)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData[key];
}
console.log(key, jsonData[key]);
}
for of loop would run a cycle for each key element that it finds inside the object but the loop is only running once. It prints out all necessary keys with values in the lower console.log function but it calls isWantedValue function only once.
Can please somebody explain that to me?
Object looks like this:
{ searchNGResponse:
{ totalCount: [ '420' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ [Object] ],
data: [ [Object] ] } }
console output:
1
called with searchNGResponse
searchNGResponse { totalCount: [ '416' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ { 'xxx': [Object] } ],
data: [ { artifact: [Object] } ] }
Edit: updated
Your jsonData object has only one key, as the console output shows: searchNGResponse.
What you want is the keys of that object, the searchNGResponse object, specifically jsonData.searchNGResponse like this
for (const key of Object.keys(jsonData.searchNGResponse)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData.searchNGResponse[key];
}
console.log(key, jsonData.searchNGResponse[key]);
}

Categories