I am trying to store all the values from result.createtimestamp into the time_stamp array, however it is only returning one result. not to sure why this is?
code:
let time_stamp = []
if(!createdIssueslength ){
return null
} else {
const lineChartData = createdIssueslength.map(result => {
time_stamp = result.create_timestamp
console.log('from console', result.create_timestamp)
})
}
console.log( 'time_stamp array',time_stamp);
You are overriding your time_stamp for every iteration you are not adding the elements.
Do this instead:
let time_stamp = []
if(!createdIssueslength ){
return null
} else {
time_stamp = createdIssueslength.map(result => result.create_timestamp)
}
console.log( 'time_stamp array',time_stamp);
On createdIssueslength.map function, you have assigned result.create_timestamp value to time_stamp variable.
So time_stamp value will be updated to the create_timestamp of each item.
To store all create_timestamp, it is needed to add that value to the array as follows.
const lineChartData = createdIssueslength.map(result => {
time_stamp.push(result.create_timestamp); // This part should be updated.
console.log('from console', result.create_timestamp)
})
Related
I am defining my variable as array and pushing values inside it.
When I try to access this variable inside another method and apply push function on it, It says, Push is not defined.
When I check the typeof of this variable it shows as String.
Any suggestions?
recipients = [];
....
handleEmailChange(event) {
const {name , value , dataset: {recipientIndex} } = event.target;
this.toAddresses[recipientIndex][name] = value;
}
handleChange(event) {
this.recipients = event.detail.value;
}
handleSend(event){
this.toAddresses.forEach ( (address) => {
const email = address.emailAddress;
this.recipients.push(email);
I suppose it caused because in the function handleChange you wrote this.recipients = event.detail.value where event.detail.value is probably a string type.
Is there way to change the index dynamically? or rebuild this object to where the 1 will be the Id of what ever object get passed into the function? Hope this makes sense.
export const createTree = (parentObj) => {
//keep in memory reference for later
const flatlist = { 1: parentObj }; <---- change the 1 based on parent.Id
...
}
My attempt thinking it would be easy as:
const flatlist = { parentObj.Id: parentObj };
Use computed property names to create a key from an expression:
const createTree = (parentObj) => {
const flatlist = { [parentObj.id]: parentObj };
return flatlist;
}
console.log(createTree({ id: 1 }));
So I have a function that has an JSON within it, and it's value consists in a key-pair in which the key is recieved as parameter and the value is the return of another function, like shown below.
const normalizeKeyValuePair = (key, value) => {
const propertyHandler = {
speed: normalizeSpeed(value),
actions: normalizeActions(value)
};
return [normalizeField(key), propertyHandler[key] || normalizeValue(value)];
};
The problem is with the actions key. Every single key parameter that normalizeKeyValuePair recieves is thrown into actions and goes to normalizeActions. How can I prevent this from happen?
To understand why this is a problem, this is normalizeActions. When actions is an primitive, JS throws an error.
const normalizeActions = actions => {
const normalizedActions = [];
for(let action of actions) {
normalizedActions.push([action.name, action.desc]);
}
return normalizedActions;
}
Thanks in advance. Let me know if needs more information!
Every time normalizeKeyValuePair is invoked, it will call normalizeActions(value) when it creates propertyHandler.
This should do what you intended:
const propertyHandler = {
speed: normalizeSpeed,
actions: normalizeActions
};
const normalizeKeyValuePair = (key, value) => {
const ph = propertyHandler[key];
return [normalizeField(key), (ph && ph(value)) || normalizeValue(value)];
};
It seems like the logic is all wrong. You should check key before calling the normalize functions, and only call the appropriate one.
const normalizeValuePair(key, value) {
let normalKey = normalizeField(key);
let normalValue;
switch(value) {
case 'speed':
normalValue = normalizeSpeed(value);
break;
case 'actions':
normalValue = normvalizeActions(value);
break;
default:
normalValue = normalizeValue(value);
}
return [normalKey, normalValue];
}
I need to create a javascript object using values stored in an array. Every value should be a new key inside the previous one. What would be the best approach to achieve this?
var option = ['level_1','level_2','level_3','level_4'];
$.each( option, function( key, value ) {
// ....
});
// I'm trying to get this result
var result = {
'level_1': {
'level_2': {
'level_3': {
'level_4':{}
}
}
}
}
You can use reduceRight for this, with the ES6 computed property name syntax.
const option = ['level_1','level_2','level_3','level_4'];
const obj = option.reduceRight( (acc, lvl) => ({ [lvl]: acc }), {});
console.log(obj);
In traditional function syntax it would be:
const obj = option.reduceRight(function (acc, lvl) {
return { [lvl]: acc };
}, {});
You have to keep track of where to put the next key. So, create a variable and initially set it to result, then on each pass through the array, move where that variable points to.
var option = ['level_1','level_2','level_3','level_4'];
var result = {};
var nextKeyGoesHere = result;
option.forEach( function( value ) {
nextKeyGoesHere[value] = {};
nextKeyGoesHere = nextKeyGoesHere[value];
});
console.log(result);
Can use Array#reduce()
var option = ['level_1','level_2','level_3','level_4'];
var res = {};
option.reduce((o, key) => (o[key] = {} , o[key]), res)
console.log(res)
you can use any of the other answers that use Array#reduce, however, if you'd like a recursive version here it is:
function _makeTree(arr, index, subtree){
if(index < arr.length){
subtree[arr[index]] = {};
return _makeTree(arr, index+1, subtree[arr[index]])
}
else return;
}
function makeTree(arr){
var tree = {};
_makeTree(arr, 0, tree)
return tree;
}
var arr = ['level_1','level_2','level_3','level_4'];
console.log(makeTree(arr));
i have understand that i need to change the global scope of this, because in the loop this refers to the window object. But if i try to define a variable in my foreach loop via a function its not working and i dont know why although my functio returns the correct value :(
// simple class for xml import
function io() {
this.vertexes = [];
this.getVertexByID = function(id) {
this.vertexes.forEach(function(entry) {
if (id == entry.id) {
// correct element found, displayed and returned
console.log(entry);
return entry;
}
});
}
this.importXML = function(xmlString) {
cells = this.xmlToJson(xmlString);
var parent = graph.getDefaultParent();
var _this = this;
graph.getModel().beginUpdate();
try {
// addEdges
cells.XMLInstance.Edges.Relation.forEach(function(entry) {
// both will be empty but i dont understand why :(
fromVertex = _this.getVertexByID(entry.fromNode);
toVertex = _this.getVertexByID(entry.toNode);
var e1 = graph.insertEdge(parent, null, '', fromVertex, toVertex);
});
} finally {
graph.getModel().endUpdate();
}
}
Returning a value in a forEach callback has no effect. It certainly is not the return value of the function that the forEach is part of.
So change this:
this.vertexes.forEach(function (entry) {
if(id==entry.id){
//correct element found,displayed and returned
console.log(entry);
return entry;
}
});
to this:
return this.vertexes.find(function (entry) {
return id==entry.id;
});