I'm using vuejs and laravel.
In component, i have:
data(): {
return {
data: []
}
}
After fetching, i have this.
I want to load more data if user scroll, so i have to add new object into data.
I tried Object.assign, push... but the properties had be overwritten.
I also loop the data and add new object but not work either...
I want something like:
obj1 = {0: value1, 1: value2};
obj2 = {0: value3, 1: value4};
=> obj = {0: value1, 1: value2, 3: value3, 4: value4};
Any idea? Tks!
You can extract the values from the object using Object.values(), then join both of the values using array#concat then using Object.assign() create your object.
const obj1 = {0: 'value1', 1: 'value2'},
obj2 = {0: 'value3', 1: 'value4'},
result = Object.assign({}, Object.values(obj1).concat( Object.values(obj2)));
console.log(result);
You can also use array#reduce instead of Object.assign.
const obj1 = {0: 'value1', 1: 'value2'},
obj2 = {0: 'value3', 1: 'value4'},
result = Object.values(obj1).concat( Object.values(obj2)).reduce((r,v,i) => (r[i] = v, r), {});
console.log(result);
data:function(){
return {
data: []
}
}
Now you can add element either by
this.data.push(object);
or You can concat another array like this -
this.data = this.data.concat(anotherArray);
After Updating the question -
/* For the case in question, you would do: */
Object.assign(obj1, obj2);
/** There's no limit to the number of objects you can merge.
* All objects get merged into the first object.
* Only the object in the first argument is mutated and returned.
* Later properties overwrite earlier properties with the same name. */
let obj = Object.assign({}, obj1, obj2, obj3, etc);
Related
How to get values from an object in nodejs?
let person = {temperature:[35,36],humidity:[85,90,89};
let y = req.query.id; (which is `temperature`)
console.log(person.y);
In order to get values from a nodejs object you can do it in the following way
Getting it directly person.temparature or person[myvar]
Or by iteration:
for (const [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`); // key = 'temperature', value = [35,36] for the first entry (temperature)
}
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
I have a list of user ids like so: ["1111","5555","1111","8983",...]. I then compute the distribution of the frequency of the ids. But somehow adding the size of the distribution bins is smaller than the user set.
function histogram(List){
var d = {};
for(const x of List){
if (x in d){
d[x]+=1;
}
else{
d[x]=1;
}
}
return d
}
var featureuserids = f1_users.concat(f2_users,f3_users,f4_users)
var featureusers = [...new Set(featureuserids)];
const featurehist = histogram(Object.values(histogram(featureuserids)))
const n_featureusers = featureusers.length
Here is an example output.
Feature Users: 17379
Feature Hist: { '1': 16359, '2': 541, '3': 93, '4': 6 }
What is my mistake?
I have found the answer. One of my Lists (f1_users) had saved the ids as int, while the others were in string format. Therefore they were counted double in the set. After converting them all to string the issue was fixed.
i have this in a collection:
resul = {
'name': 'bob',
'position': 'ceo',
}
when i run a python code again with a different position value a new data , it's overwriting the position and i dont want it to
result i want:
resul = {
'name':'bob',
'position':'ceo',
'age':25,
}
result i am getting:
resul = {
'name':'bob',
'position':'co-founder',
'age':25,
}
code using:
info_dic={}
info_dict.update(resul)
infoREferenc = db.collection(u'test').document(u'bob')
infoREferenc.set({"general":info_dic}, merge=True)
If the field(ex. position,age,name) is existing in your map(general) it will just update the value of the field. If not, it will write the field and insert with its value. I suggest add a new field in your map for additional position, for example:
info_dic={
u'name': u'bob',
u'position':u'ceo',
u'secondary-position': u'co-founder',
'age': 26
}
infoREferenc = db.collection(u'test').document(u'bob')
infoREferenc.set({'general':info_dic}, merge=True)
I need some help,
I've got a json with some parameters inside of it, actually 2 but one day we may add some more in it.
I want to find between some object in an array the right one thanks to all parameters in the json
Am i using the right method ?
to be clearer, i want the param.t to match with the element.t, and the param.tid to match with the element.tid and if moving forward one more parameter cd1 is added to the JSON, this param.cd1 will match with element.cd1
thanks for the time !
const array1 = [{"t":"pageview","de":"UTF-8","tid":"UA-xxxxxxxxxx-17","cd1":"Without cookie"},{"t":"timing","de":"UTF-8","tid":"UA-xxxxxxxx-1","cd1":"France"}];
const param = { t: 'pageview', tid: 'UA-xxxxxxxxxx-17' }
for (let [key, value] of Object.entries(param)) {
console.log(`${key}: ${value}`);
}
const obj = array1.find(element => element.t == param.t);
If I am following correctly, you want to compare an array of objects to an object and based on some keys in 'param' object you want to filter out your array1.
const array2 = [{"t":"pageview","de":"UTF-8","tid":"UA-xxxxxxxxxx-17","cd1":"Without cookie"},{"t":"timing","de":"UTF-8","tid":"UA-xxxxxxxx-1","cd1":"France"}];
const param1 = { t: 'pageview', tid: 'UA-xxxxxxxxxx-17' }
const test = array2.find(checkExist);
const checkExist = el => {
return el.t == param1.t && el.tid == param1.tid; // here you can add your keys in future
}
So my goal is to have an object variable that will be empty at the start but as the code starts running it would get filled up with data from other varibales. When it gets filled up it should look like this:
var fruits = [banana, apple, ...];
var colors = [yellow, green, ...];
var calories = [300, 250, ...]
//the JSON object
{
banana :
{
"color" : "yellow",
"calories" : 300
},
apple :
{
"color" : "green",
"calories" : 250
},
...
}
As you can see all of the data is supposed to be pulled from other variables and this is where I bump into problems. I've tried the following:
var object.fruits[0] = {colors : calories};
//also tried this
var object.fruits[0] = "{""'" + (colors[0]) + "'":+calories[0]+"}";
//and tried many other things...
I've been failing to counter this for at least an hour now and what makes it worse is that some data is supposed to come from other JSON objects. Any idea how to make it work? Also note that having them in an object array is not a option as the list will be HUGE and therefore the time efficiency will be very poor.
Maybe try something like this
res = {}
fruits.map((key, index) => {
res[key] = {
'color': colors[index],
'calories': calories[index]
}
})
You can do like this but yeah put validations to make sure all three arrays are of equal length.
Whenever you want to add a property to an Object where the property value is a value of another variable it is better to use the bracket notation to add the properties to the object [] as used below.
One more thing better use let and const in place of var.
Finally you can use JSON.stringify() to convert into JSON String from the Javascript Object.
'use strict';
const fruits = ['banana', 'apple'];
const colors = ['yellow', 'green'];
const calories = [300, 250];
const fruits_object = {};
for (let i = 0; i < fruits.length; i++) {
fruits_object[fruits[i]] = {};
fruits_object[fruits[i]]['color'] = colors[i];
fruits_object[fruits[i]]['calories'] = calories[i];
}
console.log(JSON.stringify(fruits_object));
Just do it normally like so:
color: colors[0]
and then call JSON.stringify on the entire object like so
JSON.stringify({color: colors[0]})