I am semi-new to javascript and would like to create something of this sort - a list of objects like this:
{ ID: 1, Field1: val1, Field2: val2, Field 3: val3 }
I know how to set this up if I create it statically but not sure how to handle it dynamically. In a given function, I will know the ID and one of the fields, but there are 3 separate functions to find each field/value so I need to update the objects with the fields/values as I learn them (hope this makes sense)
Can this be done and if so could someone help me with how or point me towards what I'm looking for? Thank you in advance for any help!
EDIT: I think my post was too vague, here is what I'd like to do:
When in function1, I will know the ID, let's say 1, & someField with a value of 'val' for example. So I will add this to an array.
In function2, I will know the ID, still 1, & differentField with a value of 2 for example. I want to access the object from before & add this new field/value to it (there will be more than one id/ object just using this as a base)
So in the array I want to be able to access the object by the ID & the known fields for that object
1 Answer 1
Here is a small example that I think might help you. This creates objs with dynamic names and values and adds them to an array.
var myArray = [];
for (var i = 0; i < 3; i++) {
var myObj = {};
for (var x = 0; x < 3; x++) {
myObj["Field" + x] = "val" + x;
}
myArray.push(myObj);
}
console.log(myArray)
Edit after Op's edit
Here is another example after your update. If I understood you then I think you really only need 1 function that is essentially an update to the array. Your JSON was a bit off too I think. In the example below The ID will reference an object that has a fields array where your "list" will be stored.
var myArray = [];
var testObj = {};
testObj.id = 1;
testObj.fields = [];
myArray.push(testObj)
// add myValue to the already existing obj with ID 1
addValue(1, "myValue");
// add myValue2 to the non-existent id 2 obj (it will be dynamically created)
addValue(2, "myValue2");
function addValue(id, val){
var foundID = false;
myArray.forEach(function(obj){
if(obj.id === id){
var newObj = {};
newObj["Field" + id] = val;
obj.fields.push(newObj);
foundID = true;
}
});
// if we did not find an obj with the passed in ID
// then create and initialize it
if(!foundID){
var newIDObj = {};
newIDObj.id = id;
var fieldsArray = [];
var newFieldsObj = {};
newFieldsObj["Field" + id] = val;
fieldsArray.push(newFieldsObj);
newIDObj.fields = fieldsArray;
myArray.push(newIDObj);
}
}
console.log(myArray);
// How to access a value knowing the ID and the fieldName
var result = getValue(1, "Field1");
// If no result is found, undefined will be returned
// This assumes Field Names are all unique!
function getValue(id, fieldName) {
var result;
myArray.forEach(function(currentObj) {
if (currentObj.id === id) {
currentObj.fields.forEach(function(currentField) {
if (typeof currentField[fieldName] !== "undefined") {
result = currentField[fieldName];
}
});
}
});
console.log("The value with ID: " + id + " and field name: " + fieldName + " is: " + result)
return result;
}
obj["Field" + num] = ....