Push values from nested objects - javascript

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)

Related

how to iterate parent to child objects and pushed into an array javascript (vuejs)

I have json data like this.
var obj= {
"id": "6",
"name": "parent",
"path": "/",
"category": "folder",
"fid":"6"
"children": [
{
//some values
},
{
//some other values
}
]
}
how to iterate and push it into an new array.
type declaration
getEntry: Array<Object> = []
pushing into an array method
get addedEntry() {
let files = []
this.getEntry = files.push(this.obj)
}
But, i am getting type error. How to push this object into an array or make it array.?
The push method returns a Number representing the new value of the array. That's why you are getting a TypeError (you are assigning a Number to an Array of Objects).
You should do the following instead.
get addedEntry() {
let files = []
files.push(this.obj)
this.getEntry = files
}
Here's the docs entry for the push method in JavaScript.

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

NgFor only supports binding to Iterables such as Arrays in Ionic3

I wish to convert an Ionic's storage object to an array to be readable in a ion view.
I get this error : NgFor only supports binding to Iterables such as Arrays.
How to convert an object to an array ?
Do i have to forEach storage the push the data to an array ?
This is the object :
Regards
Frank
You can use ES6's Object.values to get an array of values:
var obj = {
"1": { prop: "xxx" },
"2": { prop: "yyy" },
"3": { prop: "zzz" }
}
var arr = Object.values(obj);
console.log(arr);
I try an approch to get an array of the storage ionic object like this:
this.storage.forEach((value: string, key: string, index: number) => {
//console.log(key);
console.log('value',value);
});
It returns an array of the object but with additional values !
Strange .....
Solution from here

Underscore recursive groupBy array of string arrays

I'm trying to combine and group an array with a bunch of flat arrays that contain only strings, no objects.
So my array looks something like this:
var array = [
["MotherNode", "Node1", "ChildNode1", "ChildOfChildNode1"],
["MotherNode", "Node1", "ChildNode2", "ChildOfChildNode2"],
["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode3"],
["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode4"],
["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode5"],
["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode5"]
]
Im doing this in javascript/angularjs and so far I've gathered that the best solution is probably to use underscore.js groupBy/combine methods. However most of the examples that i can find are dealing with arrays of objects where they can group them together by using a value's key. And I'm not good enough with algorithms yet to be able to figure this out on my own.
The array I'm dealing with can have hundreds of values and the result array could get 5-10 levels deep.
The result I'd like by parsing the above array would look something like this:
var result= {
"MotherNode": [{
"Node1":[{
"ChildNode1":"ChildOfChildNode1"
},{
"ChildNode2":"ChildOfChildNode2"
},{
"Node2":[{
"ChildNode3":["ChildOfChildNode3","ChildOfChildNode4"]
},{
"Node3":[{
"ChildNode4":"ChildOfChildNode5"
}
]
}
So does anyone have any clue how this can be done? I'm completely out of ideas.
I solved this using _.reduce grouping wasnt the way to go
var result = _.reduce(array,function(memo, val){
var tmp = memo;
_.each(val, function(fldr){
if(!_.has(tmp, fldr)){
tmp[fldr] = {}
}
tmp = tmp[fldr]
})
return memo
},{})
the end leaf wont be set as a value but it should be easy to change that behavior to whatever suits you use case
{ MotherNode:
{ Node1:
{ ChildNode1: { ChildOfChildNode1: {} },
ChildNode2: { ChildOfChildNode2: {} } },
Node2: { ChildNode3: { ChildOfChildNode3: {}, ChildOfChildNode4: {} } },
Node3: { ChildNode4: { ChildOfChildNode5: {} } } } }

Return Multidimensional Array

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);

Categories