I do not know how to implement the reduce function.
I have output from my map a document as ...
for example two documents
key "_id":"AD"
"values"
{
"numtweets" : 1,
"hastags" : {
"OPINIÓ" : 1,
"debat" : 1,
"inexistent" : 1,
"tampoc" : 1,
"promet" : 1,
"gaire" : 1,
"diàleg" : 1
}
}
and other
{
"numtweets" : 1,
"hastags" : {
"other" : 1,
"debat" : 1,
"inexistent" : 1,
"another" : 1,
}
}
I need a function that reduces all sum so that
"numtweets" : 2,
"dicwords" : {
"OPINIÓ" : 1,
"debat" : 2,
"inexistent" : 2,
"tampoc" : 1,
"promet" : 1,
"gaire" : 1,
"diàleg" : 1,
"other" : 1,
"another" : 1
},
"lisuser" : {
"user2" : 1
}
if my json values was only nuwtweets the reduce is
function(key, values) {
return Array.sum(values);
};
if mi json values only was hastags (without numtweets and lisuser) the reduce function was
var r = function(key, values) {
result = {}
values.forEach(function(val) {
for (hashtag in val) {
if (hashtag in result) {
result[hashtag] = result[hashtag] + val[hashtag]
} else {
result[hashtag] = val[hashtag]
}
}
});
return(result)
};
But I don't know how mix the 2 reduce function.
My level of mongodb and JavaScript is very low.
Thanks
Try this:
let data = values.reduce((finalObj, ele) => {
finalObj.numtweets += ele.numtweets
for (key in ele.hastags) {
if(ele.hastags.hasOwnProperty(key)) {
if(finalObj.hastags[key])
finalObj.hastags[key] += ele.hastags[key]
else
finalObj.hastags[key] = ele.hastags[key]
}
}
return finalObj
})
console.log(data)
Where values is array for objects which you want reduce to one object
Related
I have an array. The data in the array is in the following format.
var test = [
{
"a" : {
"order" : 100,
}
},
{
"b" : {
"order" : 10,
}
},
{
"c" : {
"order" : 1,
}
},
];
I want to sort this data according to order value. Is there any way to do this?
You can use Object.values to get the first property value and access the order property on that to compare.
let test=[{a:{order:100}},{b:{order:10}},{c:{order:1}}];
test.sort((a, b)=>Object.values(a)[0].order - Object.values(b)[0].order);
console.log(test);
For a more generalized solution, you can create a key extractor function to get the value to compare by.
let test=[{a:{order:100}},{b:{order:10}},{c:{order:1}}];
const getOrder = x => Object.values(x)[0].order;
test.sort((a, b)=>getOrder(a) - getOrder(b));
console.log(test);
You can use JS custom sort from Array.prototype.sort(), reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Then you can sort by comparing the two element's order, but you still need to determine it's key/attribute (e.g.: a or b or c)
Here, you can use Object.keys() function and take the first key in the object, reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Here's a working example:
var test = [
{
"a" : {
"order" : 100,
}
},
{
"b" : {
"order" : 10,
}
},
{
"c" : {
"order" : 1,
}
},
];
//console.log(test);
test.sort((firstEl, secondEl) => {
var key1 = Object.keys(firstEl)[0];
var key2 = Object.keys(secondEl)[0];
return firstEl[key1].order - secondEl[key2].order
} );
console.log(test);
Output:
[
{
"c": {
"order": 1
}
},
{
"b": {
"order": 10
}
},
{
"a": {
"order": 100
}
}
]
Original JSON
var dataJson = [{
"MID" : "NTE",
"TNAME" : "gGAR",
"MVALUE" : 6
}, {
"MID" : "NTP",
"TNAME" : "gGAR",
"MVALUE" : 50
}, {
"MID" : "NTR",
"TNAME" : "gGAR",
"MVALUE" : 12
}, {
"MID" : "NTE",
"TNAME" : "gRRR",
"MVALUE" : 1
}, {
"MID" : "NTP",
"TNAME" : "gRRR",
"MVALUE" : 100
}, {
"MID" : "NTR",
"TNAME" : "gRRR",
"MVALUE" : 1
}];
Need to group by "TNAME" and after all the group taking first three objects based on "MID" and modify the JSON structure like
Expected output JSON:
var Convert = [
{
"GGARMVALUENTE":6,
"GGARMVALUENTP":50,
"GGARMVALUENTR":12,
"GRRRMVALUENTE":1,
"GRRRMVALUENTP":100,
"GRRRMVALUENTR":1
}
]
Finally it works according to your requirements. Just Solved Puzzling Question. :)
function restructure (data) {
let convert = []
let buffer = null
let pairs = data.map(d => {
return [`${d.TNAME.toUpperCase()}MVALUE${d.MID}`, d.MVALUE]
})
pairs.forEach((p, i) => {
let name = p.shift()
let value = p.shift()
let pair = {
[name]: value
}
if (buffer && buffer[name]) {
convert.push(buffer)
delete buffer
buffer = pair
} else {
if (buffer) {
Object.assign(buffer, pair)
} else {
buffer = pair
}
if (pairs.length === i + 1) {
convert.push(buffer)
delete buffer
}
}
})
return convert
}
console.log(restructure(dataJson))
i have an object like this in my console:
ObjectName1 : Array(3)
0 : { id : 1, name : 'foo' },
1 : { id : 2, name : 'foo-2' },
2 : { id : 3, name : 'foo-3' },
ObjectName2 : Array(3)
0 : { id : 1, foo : 'bar' },
1 : { id : 2, foo-2 : 'bar-2' },
2 : { id : 3, foo-3 : 'bar-3' },
and as usually if we want to get the name, just write : ObjectName1[key].name right ?
now if i want to get the key from ObjectName2 (foo, foo-2, foo-3) how to get the key from ObjectName2 using the value from ObjectName1 ?
i have written like this :
// just say there is an each above this comment
var name = ObjectName1[key].name;
var bar = ObjectName2[key]+"."+name;
// end each
but it just showed
[Object object].foo
[Object object].foo-2
[Object object].foo-3
the output should be like this :
bar
bar-2
bar-3
it is possible doing like i want to do ? help me please if it is possible
any help will be very appreciated.
*note : i'm not sure what is the case name in my problem, so forgive me if the title went wrong
thanks
Try this one. Loop through each object in ObjectName1 object and get the name in appropriate index, this name will be the key for the ObjectName2 object. Then use that key to print the appropriate value from ObjectName2
var ObjectName1 = [{'id' : 1, 'name' : 'foo'}, {'id' : 2, 'name' : 'foo-2'}, {'id' : 3, 'name' : 'foo-3'}];
var ObjectName2 = [{'id' : 1, 'foo' : 'bar'}, {'id' : 2, 'foo-2' : 'bar-2'}, {'id' : 3, 'foo-3' : 'bar-3'}];
for(var i = 0; i < ObjectName2.length; i++){
console.log(ObjectName2[i][ObjectName1[i]['name']]);
}
Something like this?
var name = ObjectName1[key].name;
ObjectName2.forEach(function(a) {
if (a.keys().includes(name)) {
var bar = a[name];
// then do what you want with bar
}
}
As commented, your key is an object. Hence, it is showing [Object object].foo-3.
You will have to use 2 loops and check if the key is inside current object. If yes, print it, else continue.
var ObjectName1 =[
{ id : 1, name : 'foo' },
{ id : 2, name : 'foo-2' },
{ id : 3, name : 'foo-3' },
]
var ObjectName2 = [
{ id : 1, foo : 'bar' },
{ id : 2, 'foo-2' : 'bar-2' },
{ id : 3, 'foo-3' : 'bar-3' },
];
ObjectName1.forEach(function(obj){
ObjectName2.forEach(function(obj2){
var key = obj.name;
if(key in obj2){
console.log(obj2[key])
}
})
})
now if i want to get the key from ObjectName2 (foo, foo-2, foo-3) how to get the key from ObjectName2 using the value from ObjectName1 ?
If you know those are parallel arrays (where [0] in one array is intentionally a match for [0] in the other array), you can simply loop through:
ObjectName1.forEach(function(entry1, index) {
var value = ObjectName2[index][entry1.name];
console.log(entry1.name + " = " + value);
});
Example:
var ObjectName1 = [
{ id : 1, name : 'foo' },
{ id : 2, name : 'foo-2' },
{ id : 3, name : 'foo-3' }
];
var ObjectName2 = [
{ id : 1, "foo" : 'bar' },
{ id : 2, "foo-2" : 'bar-2' },
{ id : 3, "foo-3" : 'bar-3' }
];
ObjectName1.forEach(function(entry1, index) {
var value = ObjectName2[index][entry1.name];
console.log(entry1.name + " = " + value);
});
That assumes you know they're parallel arrays.
If not, you have to search for it. Array.prototype.findIndex will return the index of the first element where a callback returns true:
ObjectName1.forEach(function(entry1) {
console.log("entry1.name = " + entry1.name);
var index = ObjectName2.findIndex(function(entry2) {
// See if entry2 contains a key with that value
return entry1.name in entry2;
});
console.log(index == -1 ? "Not found" : ("Found at index #" + index + ", value = " + ObjectName2[index][entry1.name]));
});
Example:
var ObjectName1 = [
{ id : 1, name : 'foo' },
{ id : 2, name : 'foo-2' },
{ id : 3, name : 'foo-3' }
];
var ObjectName2 = [
{ id : 1, "foo" : 'bar' },
{ id : 2, "foo-2" : 'bar-2' },
{ id : 3, "foo-3" : 'bar-3' }
];
ObjectName1.forEach(function(entry1) {
console.log("entry1.name = " + entry1.name);
var index = ObjectName2.findIndex(function(entry2) {
// See if entry2 contains a key with that value
return entry1.name in entry2;
});
console.log(index == -1 ? "Not found" : ("Found at index #" + index + ", value = " + ObjectName2[index][entry1.name]));
});
If you don't really need the key (e.g., index) of the matching object in ObjectName2, just the object, use find instead:
ObjectName1.forEach(function(entry1) {
console.log("entry1.name = " + entry1.name);
var entry = ObjectName2.find(function(entry2) {
// See if entry2 contains a key with that value
return entry1.name in entry2;
});
console.log(!entry ? "Not found" : ("Found, value is " + entry[entry1.name]));
});
Example:
var ObjectName1 = [
{ id : 1, name : 'foo' },
{ id : 2, name : 'foo-2' },
{ id : 3, name : 'foo-3' }
];
var ObjectName2 = [
{ id : 1, "foo" : 'bar' },
{ id : 2, "foo-2" : 'bar-2' },
{ id : 3, "foo-3" : 'bar-3' }
];
ObjectName1.forEach(function(entry1) {
console.log("entry1.name = " + entry1.name);
var entry = ObjectName2.find(function(entry2) {
// See if entry2 contains a key with that value
return entry1.name in entry2;
});
console.log(!entry ? "Not found" : ("Found, value is " + entry[entry1.name]));
});
I have a object.
var dl_items;
After a loop for inputing data:
dl_items[code] = itemObject;
I have a array:
dl_items : {
"code_A" : { "index" : 1, "status" : 2, "name" : A_data},
"code_B" : { "index" : 2, "status" : 0, "name" : B_data},
"code_C" : { "index" : 3, "status" : 1, "name" : C_data},
"code_D" : { "index" : 4, "status" : 2, "name" : D_data},
"code_E" : { "index" : 5, "status" : 4, "name" : E_data}
}
Now I want to remove "dl_items[code_D]" and insert it into after "code_A" (index 2) for result like :
dl_items : {
"code_A" : { "index" : 1, "status" : 2, "name" : A_data},
"code_D" : { "index" : 4, "status" : 2, "name" : D_data},
"code_B" : { "index" : 2, "status" : 0, "name" : B_data},
"code_C" : { "index" : 3, "status" : 1, "name" : C_data},
"code_E" : { "index" : 5, "status" : 4, "name" : E_data}
}
I try to use "delete" after using a loop to find index of code_D:
delete dl_items[code_D];
and it successful removed but how can i insert code_D into his new index ?
Edit : Thanks all everyone to help me understand more about array.
Since object doesn't have an order, you need to convert your current implementation into array:
var dl_items = [];
When you need to add an item to the array:
dl_items.push({ code: code, item: itemObject });
Now, the similar data as array from your question is:
dl_items: [
{ code :"code_A", item: { index: 1, status: 2, name: "A_data" } },
{ code :"code_B", item: { index: 2, status: 0, name: "B_data" } },
{ code :"code_C", item: { index: 3, status: 1, name: "C_data" } },
{ code :"code_D", item: { index: 4, status: 2, name: "D_data" } },
{ code :"code_E", item: { index: 5, status: 3, name: "E_data" } },
]
In order to move the entry with code_D after the entry with code_A, use the following:
var codeDEntry = dl_items[3];
dl_items = dl_items
.filter(function(entry) {
return entry !== codeDEntry;
})
.splice(1, 0, codeDEntry);
Hope this helps!
You can make a temp var like this :
tempItem = dl_items.code_D;
dl_items.code_D = dl_items.code_B;
dl_items.code_B = tempItem;
What you have here is an object, not an array. Therefore, there is no concept of an index here.
You can map your object keys into an array as follows:
let array = Object.keys(dl_items);
You can then reorder their positions.
In JavaScript code, I have the following enum defined:
MyMessageIds = {
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
}
In a JavaScript function, I would like to be able to supply the string representation of an enum key (i.e. "MoveId") and return its integer value of 4. So how could I do this?
Just use bracket notation:
var MyMessageIds = {
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
};
function getValue(key) {
return MyMessageIds[key];
}
You could create some utility methods which take an object (enum) that figures out how to get the keys/values.
var MyMessageIds = {
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
}
function getEnumKeys(enumType) {
return Object.keys(MyMessageIds);
}
function getEnumValues(enumType) {
return getEnumKeys(enumType).map(function(key) {
return enumType[key];
});
}
function getEnumValue(enumType, key) {
return enumType[getEnumKeys(enumType).filter(function(k) {
return key === k;
}).pop() || ''];
}
document.body.innerHTML = '<pre>' + JSON.stringify({
'Enum Keys' : getEnumKeys(MyMessageIds),
'Enum Vals' : getEnumValues(MyMessageIds),
'Example' : {
'MoveId' : getEnumValue(MyMessageIds, 'MoveId')
}
}, null, 4) + '</pre>';
You could also create your own class object to represent an enum which has reusable methods.
function Enum() {
this.self = arguments[0];
}
Enum.prototype = {
keys : function() {
return Object.keys(this.self);
},
values : function() {
var me = this;
return this.keys(this.self).map(function(key) {
return me.self[key];
});
},
getValueByName : function(key) {
return this.self[this.keys(this.self).filter(function(k) {
return key === k;
}).pop() || ''];
},
getNameByValue : function(value) {
var me = this;
return this.keys(this.self).filter(function(k) {
return me.self[k] === value;
}).pop() || null;
}
};
var MyMessageIds = new Enum({
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
});
document.body.innerHTML = '<pre>' + JSON.stringify({
'Enum Keys' : MyMessageIds.keys(),
'Enum Vals' : MyMessageIds.values(),
'Example' : {
'MoveId' : MyMessageIds.getValueByName('MoveId'),
'Val(3)' : MyMessageIds.getNameByValue(3)
}
}, null, 4) + '</pre>';