I make the following axios call and get the response below it:
axios.get('/[my url]/', {
params: {
myParam: this.items
}
})
Response:
0 Object { index: 1, var1: 3, var2: 2, … }
index 1
var1 3
var2 2
var3 480
var4 5800000
1 Object { index: 1, var1: 4, var2: 4, … } ......
I then try to split this response up into an array of objects, with an object for each index value present, and assign that value the this.series array:
.then(response => {
let test = response.data;
let obj = [];
test.forEach(function(item) {
console.log('run: ' + item);
if(!obj[item.index]) {
obj[item.index] = {
name: item.index,
type: 'scatter',
data: []
};
}
obj[item.index].data.push(
[
parseFloat(item.var3),
parseFloat(item.var4)
]
);
delete item.index;
});
this.series = obj;
})
Here is the part I don't understand. The code above does not work and prints this to console:
run: [object Object]
so it only runs once.
When I replace the console.log with console.log('run: ' + item.var4);
it then runs the right amount of times, but the obj variable still does not populate properly.
Any insights as to why this is happening?
Related
Iam having an array of object named finalArr and and object named replaceJsonData. If replaceJsonData contains add =1 , then value of REQUEST_TYPE in finalArr should also become 1
finalArr = [{
REQUEST_TYPE: 'add',
TIMESTAMP: '1671636661867',
}, ]
let replaceJsonData = {
"REQUEST_TYPE": {
'add': 1,
'modify': 2
}
}
I tried like this way , but value itself is in the form a key
finalArr.map((ele)=>{
Object.entries(replaceJsonData ).forEach(
([replaceDataKey, replaceDataValue]) => {
if (ele[replaceDataKey]) {
ele[replaceDataKey]=replaceDataValue
}
}
)
});
Expected Output:
finalArr = [{
REQUEST_TYPE: 1,
TIMESTAMP: '1671636661867',
}, ]
Use the property of replaceDataValue that matches the old value of the property being updated: replaceDataValue[ele[replaceDataKey]]
let finalArr = [{
REQUEST_TYPE: 'add',
TIMESTAMP: '1671636661867',
}, ]
let replaceJsonData = {
"REQUEST_TYPE": {
'add': 1,
'modify': 2
}
};
finalArr.map((ele) => {
Object.entries(replaceJsonData).forEach(
([replaceDataKey, replaceDataValue]) => {
if (ele[replaceDataKey]) {
ele[replaceDataKey] = replaceDataValue[ele[replaceDataKey]];
}
}
)
});
console.log(finalArr);
am trying to make JSON content dynamic and the idea is to have a simple json content
{
"name": "##var1##"
"lastname":"##var2##"
}
and have another json content
{
"var1": "samy"
"var2":"something"
}
the idea is to use the second JSON content to fill in the variables in the first JSON and become
{
"name": "samy"
"lastname":"something"
}
I did search for a library to help me achieve that and I did file json-variables but unfortunately, it doesn't work the way I want instead it does use variables from the same JSON file
const jv = require("json-variables");
var res = jVar({
a: "some text %%_var1_%% more text %%_var2_%%",
b: "something",
var1: "value1",
var2: "value2",
});
console.log("res = " + JSON.stringify(res, null, 4));
// ==> {
// a: 'some text value1 more text value2',
// b: 'something',
// var1: 'value1',
// var2: 'value2'
// }
const arr1 = {
"name": "##var1##",
"lastname":"##var2##"
}
const arr2 = {
"var1": "samy",
"var2":"something"
}
function myFunction(arr1, arr2) {
return Object.entries(arr1).reduce((acc, [key, value]) => {
acc[key] = arr2[value.replace(/#/g, '')];
return acc;
}
, {})
}
console.log(myFunction(arr1, arr2));
Suppose if we are having data like this, how can we use dot notations or anything to update the array(data2) inside the object data1 which is inside the array(data).
data = [
data1:
{
a:"1",
b:"2"
}
]
// another array(data2 - below) I am having I have to push that array inside
data1 as an object
data2 = [
{
c:"3"
},
{
d:"4"
}
]
The response I want as below:
data = [
data1:
{
a:"1",
b:"2"
},
data2 = [
{
c:"3"
},
{
d:"4"
}
]
]
var array = [];
array['data1'] = { 'name': 'a' }
var array2 = [{ c: 3 }, { d: 4 }];
array['data2'] = array2;
console.log(array)
OutPut:
[ data1: { name: 'a' }, data2: [ { c: 3 }, { d: 4 } ] ]
I didnt really understand your question but i think this is what you want?!
data = {
data1:
{
a: "1",
b: "2"
}
}
data2 = [
{
c: "3"
},
{
a: "3"
},
{
d: "4"
}
]
for (var key in data2) {
for (var key2 in data2[key]) {
if(data.data1[key2] != null){
console.log("data.data1 with they key" + key2 + " could have been replaced/updated with " + data2[key][key2]);
}
console.log("key " + key2 + " has value " + data2[key][key2]);
}
}
Result:
key c has value 3
data.data1 with they keya could have been replaced/updated with 3
key a has value 3
key d has value 4
Edit:
Why dont you just do
data["data2"] = data2?
I tried on myself and I got the solution. If we have to make another field in an object you just give a name like this
data.dataTwo
this will create another field with new name and now if we want to transfer or store an array in this we can simply do this
data.dataTwo = data2
This will assign that data2 named array in a new field what is given as dataTwo
that is suppose to come 1,2,3 but coming 3,3,3, how to fix that ?
Javascript updating automatically
let test = [ { id: 1 } ];
let test2 = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
let x = []
test2.forEach(i => {
test[0].id = i.id;
x.push(test[0])
});
console.log(x)
Since you are pushing the same object 3 times and at the end of the loop it will have 3 reference of the same object i.e test[0]
You can use spread syntax to copy all properties of object
let test = [{ id: 1 }];
let test2 = [{ id: 1 }, { id: 2 }, { id: 3 }];
let x = [];
test2.forEach((i) => {
test[0].id = i.id;
x.push({ ...test[0] });
});
console.log(x);
Use the spread operator:
x.push({ ...test[0] })
Basically you need to shallow clone the array because it's an object; forEach will create 3 references to the same test[0] object at the beginning of the call.
You are passing in the same reference to the array everytime. You are updating that same value too i.e., test[0].
So in the end, you have an array with three elements, all 3 pointing to the same object whose id property you have updated to the final value - test2[2].id.
You can directly push in an object with the correct id property. You will not need an extra test array as you are creating your object and pushing them on the go.
let test = [ { id: 1 } ];
let test2 = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
let x = []
test2.forEach(i => {
x.push({ id : i.id })
});
console.log(x)
How can I get an array with all the unique values based on a property name?
In my case my object looks like this and I want an array with the unique documentID's.
const file = {
invoice: {
invoiceID: 1,
documentID: 5
},
reminders: [
{
reminderID: 1,
documentID: 1
},
{
reminderID: 2,
documentID: 1
}
]
}
The result should be an array [5, 1] //The unique documentID's are 5 and 1
It doesn't seem like possible to add a property name to the Object.values() function.
You can use Set to get unique documentID.
const file = {
invoice: {
invoiceID: 1,
documentID: 5
},
reminders: [
{
reminderID: 1,
documentID: 1
},
{
reminderID: 2,
documentID: 1
}
],
payments: {
documentID : 5
}
};
var keys = Object.keys(file).map(key=>file[key].map ? file[key].map(i=>i.documentID) : file[key].documentID)
var keysFlattened= [].concat.apply([], keys);
var unique = new Set(keysFlattened);
console.log(Array.from(unique));
I use something like this that does what you want I think
const keepUniqueBy = key => (array, item) => {
if (array.find(i => item[key] === i[key])) {
return array;
} else {
return [ ...array, item ];
}
};
Then you can simply: const unique = reminders.reduce(keepUniqueBy('documentID'))
NB: It's probably low performing, but for small arrays it doesn't matter.