I have this data:
$scope.data = [
{
data1:"1",
data2:"2"
},
{
data1:"1",
data2:"2"
}
];
I want to output them in console log as:
[ [1,2], [1,2] ]
Any suggestions please.
$scope.data = [{
data1:"1",
data2:"2"
}, {
data1:"1",
data2:"2"
}].map(function (o) {
return [o.data1, o.data2];
// or, if you really need the items to be numbers:
// return [o.data1, o.data2].map(Number);
});
Yao,
Look at Lodash or ngLodash. Lodash has lots of function that do exactly what you are looking to do. ngLodash is a fork and AngularJS rewrite of Lodash.
https://github.com/rockabox/ng-lodash
https://lodash.com
Hope this helps!
Regards,
Jeff
Related
I'm trying to convert an object into single array with just key and value. I tried two different ways. Though I succeeded in my first attempt but using reduce I am unable to get the result. Please kindly let me know if there is/are better ways of achieving the same. Thank you.
var demoArr = [
{
"results": [
{
"listing_id": 10544193
},
{
"listing_id": 4535435
}
],
"results1": [
{
"listing_id": 1054419363
},
{
"listing_id": 432535435
}
]
}
];
let aaa = [];
// demoArr.map(x => { (before)
demoArr.forEach(x => { //(after)
let ss = Object.values(x);
// ss.map(y => { (before)
ss.forEach(y => { //(after)
y.forEach(k => {
aaa.push({"listing_id" : k.listing_id});
})
});
});
Resulted in the following.
[{"listing_id":10544193},{"listing_id":4535435},{"listing_id":1054419363},{"listing_id":432535435}]
Is there a better way to achieve the above? Maybe by using reduce? I tried but failed to get the result.
var ds = demoArr.reduce((a,value) => {
a[value.listing_id] = a[value.listing_id] ? a[value.listing_id] : value
return a
},{});
Here's one way use flatMap() and Object.values, then flatten the result. Using Object.values as an argument in this fashion is a shorthand for applying the map argument directly into the Object.values method and returning the result
var demoArr = [{
"results": [{
"listing_id": 10544193
},
{
"listing_id": 4535435
}
],
"results1": [{
"listing_id": 1054419363
},
{
"listing_id": 432535435
}
]
}];
let output = demoArr.flatMap(Object.values).flat();
console.log(output)
I have the following JSON Structure:
{
"list":[
{
"id":"7ccba2c3-e6df-4cb2-94b9-2f6320366ce0",
"name":"List 1",
"people":[
{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}
]
},
{
"id":"a2d1d55a-e6df-4cb2-94b9-000000011212",
"name":"List 2",
"people":[
{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}
]
}
]
}
and I need to group people in a only one array like this:
{
"people": [
{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},
{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},
{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}
]
}
using underscore groupBy function, the return remains the same.
Anyone has any idea how can I group this content above?
Thanks in advance.
You can use Array.flatMap() (or lodash's _.flatMap()) to flatten the list of people to as single array:
const data = {"list":[{"id":"7ccba2c3-e6df-4cb2-94b9-2f6320366ce0","name":"List 1","people":[{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 1","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 2","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 3","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}]},{"id":"a2d1d55a-e6df-4cb2-94b9-000000011212","name":"List 2","people":[{"id":"b6cb6317-8f89-4826-b60b-aa0ecf51b1af","name":"Person 4","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]},{"id":"f2620c63-37cc-45be-86e0-fff5d415e483","name":"Person 5","mediaAccounts":[{"socialNetwork":"twitter"},{"socialNetwork":"facebook"}]},{"id":"e8ccae28-6695-435b-ba61-dbf569e75665","name":"Person 6","mediaAccounts":[{"socialNetwork":"facebook"},{"socialNetwork":"twitter"}]}]}]}
const result = {
people: data.list.flatMap(o => o.people)
}
console.log(result)
I'm looping over object with a property set to array, which i then use in the following way:
let merged= [];
for (let sup of superObject) {
let sname = sup.superObjectName;
for (let sub of sup.subObject) {
merged.push({superObject: sname, subObject: sub.subObjectName})
}
}
Now the code above works and get job done but i feel it can be improved using Lodash and i cant get it to work. i tried using flatMap in few different ways but none seem to work, and the one in the right direction in terms of functionality wont seem like an improvement at all.
Any ideas?
UPDATE:
superObject example:
{
superObjectName: "someName",
subObject: [
{subObjectName: "someSubName"},
{subObjectName: "someSubName2"}
]
}
This does the same as your code:
const merged = _.flatMap(superObject, ({superObjectName, subObject}) =>
_.map(subObject, ({subObjectName}) => ({
superObject: superObjectName,
subObject: subObjectName
}))
);
Each value in superObject transformed to Array with map, and then flattened inside flatMap.
You can use flatMap, get props and get the desire result like this using lodash.
var data= [{
superObjectName: "someName",
subObject: [
{subObjectName: "someSubName"},
{subObjectName: "someSubName2"}
]
}];
const result = _.flatMap(data, ({ superObjectName, subObject}) =>
_.map(subObject, ({subObjectName})=> ({superObject: superObjectName, subObject: subObjectName}))
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Building on this angular recursive extend topic.
I have slightly modified the version...
var extendDeep = function(dst) {
angular.forEach(arguments, function(obj) {
if (obj !== dst) {
angular.forEach(obj, function(value, key) {
if (dst[key] && angular.isObject(dst[key])) {
extendDeep(dst[key], value);
} else if(!angular.isFunction(dst[key])) {
dst[key] = value;
}
});
}
});
return dst;
};
but found that it does not account for arrays correctly. For example, if I have a object like:
var obj = { zoo: [ 'moose', 'panda' ] };
and then I call like:
deepExtend(obj, {
zoo: [ 'panda' ]
})
at first glance you would expect it to remove the object, however, it actually ends up like:
{ zoo: [ 'panda', 'panda' ] }
now. The expected output would look like:
{ zoo: [ 'panda' ] }
This is a very simple case of course.
I'm not really looking for a 'unique' or a 'merge' like most solutions talk about. I need to add items that are missing from the left, remove items that are on the left but not on the right, and then iterate recursively and extend those objects in place.
There are many different ways to go about this, looking for feedback on a good approach.
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: {} } } } }