Return Multidimensional Array - javascript

I want to return a multiply dimensional array from a function like so but I must be writing it wrong, I cant figure out whats wrong. I want to have key and value pairs.
function Multidimensional(){
return [
"one": [
"two":[],
"three":[
"testing.png":{source:"http..."}
],
"another.png": {source:"http..."}
];
}

If you want to have key/value pairs, you should use an object.
function Multidimensional(){
return {
"one": {
"two":[],
"three":{
"testing.png":{source:"http..."}
},
"another.png": {source:"http..."}
};
}
You can access the returned data like so:
var data = Multidimensional();
console.log(data['another.png']);
// or
console.log(data.one);

Related

How can I remove the key from an array but keeping the value in the array?

I have an array like this:
language:
[
{added: "English"}
]
What I want to do is to remove the key added but I want to keep the value English in the same array.
The result I except:
language:
[
"English"
]
By far I have tried something like this:
for(let i in language) {
delete language[i].added
}
console.log(language)
This will remove the key and the value as well.
How can I remove the key, but keep the value in the same array?
If the objects just consist of a single property, added, you can use Array.map to convert the objects into scalar values:
data = {
language: [
{ added: "English" }
]
}
data.language = data.language.map(o => o.added)
console.log(data)
You aren't deleting a key, you're replacing an entry in an array. The old entry is an object with a single key and the new one is it's value:
language = [
{added: "English"}
]
for (let i in language) {
language[i] = Object.values(language[i])[0]
}
console.log(language);

Flatten inner array but assign outer properties

I have an input like this:
var input = [
{
"inner_array": [
{
"inner_data": "inner_data1"
},
{
"inner_data": "inner_data2"
}
],
"outer_data": "outer_data",
}
];
And I'd like to process it so it becomes this.
var output = [
{
"inner_data": "inner_data1",
"outer_data": "outer_data",
},
{
"inner_data": "inner_data2",
"outer_data": "outer_data",
}
];
In words: I'd like to flatten an inner array, while still keeping the outer array's properties. Does this have an easy solution (with built in lambda array functions), or should I write a function myself which handles this?
You could use .flatMap() on your input array to loop over each object, along with an inner .map() to map each inner_array to a new object. The new object can be a combination of the outer_data value along with the inner_data value. The .flatMap() method will then merge all returned objects from the inner .map() calls for each object within input into one resulting array:
const input = [{ "inner_array": [{ "inner_data": "inner_data1" }, { "inner_data": "inner_data2" } ], "outer_data": "outer_data", }];
const res = input.flatMap(obj => obj.inner_array.map(inner => ({
...inner,
outer_data: obj.outer_data
})));
console.log(res);

How to find dynamically a key values inside an object?

each API request I'm making contains different keys values inside a specific object.
How can I dynamically get the Number value of the second key? ("123112042")
"salesRanks": {
"281052": [ keepaTime, salesRank, ... ]
"123112042": [ keepaTime, salesRank, ... ]
}
Target the Object.keys and get the second index.
const data = {
salesRanks: {
"281052": [1, 1],
"123112042": [2, 2]
}
};
console.log(Object.keys(data.salesRanks)[1]);
Although an object's keys are not really meant to be ordered, you could write an object iterator and assign with destructuring like this:
const obj = {
salesRanks: {
281052: ["keepaTime", "salesRank"],
123112042: ["keepaTime", "salesRank"],
[Symbol.iterator]: function () {
return Object.keys(this).values();
}
},
};
const {
salesRanks: [, second],
} = obj;
console.log(second);

Push values from nested objects

I would like to know the best method for grabbing values from nested objects like this one:
var object = {
1: {
1: {
"names": "bob"
},
2: {
"names": "jim"
}
}
}
What function or loop would I write to push the values “bob” and “Jim” into an array?
Thank you in advance!
you can use Object.values + map
Object.values(object[1]).map(p => p.names)

javascript recursive function concat not working

I have a nested array of objects like below and I'm trying to push all the values in to a single array. all the values are located in sp->it->value or sp->it->it->value
[
{
"sp": [
{
"it":[
{"value":5}
]
},
...
],
"b": {
...
}
},
{
"sp": [
{
"it":[
{"nm":5}
]
}
],
"b": {
...
}
},
{
"sp": [
{
"it":[
{
"it":[
{"value":5}
]
}
]
}
],
"b": {
...
}
},
]
and here is what I have tried
const getValues = (js) => {
let values = []
js.map((val,i) => {
if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))
else if("value" in val) values.push(val.value)
})
return values
}
I thought I could concatenate the returned value from the recursive call since it returns an array but the above code returns empty array. Any insights?
Edit fixed the typo on sp object. It is array of objects.
Array.prototype.concat()
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
So those lines do nothing:
if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))
You need to write:
if("sp" in val) values = values.concat(getValues(val.sp))
else if("it" in val) values = values.concat(getValues(val.it))
And you should not use map if you don't use it's result. Use forEach instead.
This is because you are passing val.sp to function which is not array but it is an object and .map is a property of an array

Categories