I am trying to update multiple elements at once in a multidimensional array/object and having errors with .push and other options I have tried.
var FEtypes= {
textTemplate: {
type :"Text",
size : 50,
required : "no",
FEadd : '<input>',
label : 'Label',
top : 0,
left : 0
},
textareaTemplate: {
type : "Textarea",
cols : 30,
rows : 5,
FEadd :'<textarea>'
}
}
This works, but trying to do in one line.
FEtypes[1].left = someVariable1;
FEtypes[1].top = someVariable2;
I have been unsuccessful with:
FEtypes[1].push( {left:someVariable1, top:someVariable2} );
I keep getting an error that this is not a function or it does nothing.
I am trying to update multiple elements at once in a multidimensional array/object and having errors with .push and other options I have tried.
var FEtypes= {
textTemplate: {
type :"Text",
size : 50,
required : "no",
FEadd : '<input>',
label : 'Label',
top : 0,
left : 0
},
textareaTemplate: {
type : "Textarea",
cols : 30,
rows : 5,
FEadd :'<textarea>'
}
}
This works, but trying to do in one line.
FEtypes[1].left = someVariable1;
FEtypes[1].top = someVariable2;
I have been unsuccessful with:
FEtypes[1].push( {left:someVariable1, top:someVariable2} );
I keep getting an error that this is not a function or it does nothing.
Share Improve this question edited Nov 28, 2011 at 2:15 JCOC611 19.8k15 gold badges71 silver badges90 bronze badges asked Nov 28, 2011 at 2:11 user1009749user1009749 633 silver badges8 bronze badges 4- I'm not seeing the multidimensional array here. FETypes looks like an object with two properties, textTemplate and textareaTemplate, each of which is in turn an object. – wrschneider Commented Nov 28, 2011 at 2:15
- You can't use push on a object like that. push is used to add elements to an array. Not overwrite object properties. Whats wrong with your first example? – Ben Commented Nov 28, 2011 at 2:15
-
Array is not the same as object.
.push
is a function used for arrays (ex.["this", "is", "an", "array"]
) and not objects. – JCOC611 Commented Nov 28, 2011 at 2:15 -
Note that even if you had an array (and not a "plain" object)
.push()
would still be the wrong choice because it adds a new element to the end of an array, it doesn't update an existing element. – nnnnnn Commented Nov 28, 2011 at 3:19
5 Answers
Reset to default 6FEtypes
is an object, not an array. Push
is only available on arrays.
If you want to store this structure in an array, it would look something like:
var FEtypes= [
{type :"Text", size : 50, required : "no", FEadd :'<input>', label : 'Label', top : 0, left : 0},
{type : "Textarea", cols : 30, rows : 5,FEadd :'<textarea>' }
];
Then
FEtypes[1].left = someVariable1;
FEtypes[1].top = someVariable2;
If you want to modify multiple properties at once, the jQuery extend function will get you what you want:
$.extend(FEtypes[1], {left:someVariable1, top:someVariable2});
But I think your original structure is more suitable. Just ditch arrays, and do this:
FEtypes.textareaTemplate.left = someVariable1;
FEtypes.textareaTemplate.top = someVariable2;
Or, again, with just one line with jQuery:
$.extend(FEtypes.textareaTemplate, {left:someVariable1, top:someVariable2});
FEtypes
is an object and it has two properties textTemplate
and textareaTemplate
. It is not an array and does not have a property [1]
. As such, you can't use .push()
or [1]
.
The proper way to access the .left
and .top
parameters or the textareaTemplate property is this:
FETypes.textareaTemplate.left = someVariable1;
FETypes.textareaTemplate.top = someVariable2;
or for textTemplate
, it would be this:
FETypes.textTemplate.left = someVariable1;
FETypes.textTemplate.top = someVariable2;
Both the textareaTemplate
and textTemplate
properties are objects. If you wanted to replace the entire object (thus replacing all other properties on the object), you could do this:
FETypes.textTemplate = {left: someVariable1, top: someVariable1};
But, when doing that, you would be replacing the entire textTemplate property so it would have no other properties besides left
and top
. Any prior properties like size
or required
would be wiped out by the new assignment.
If you want to update just the left
and top
properties leaving the others in place, you have to assign to them individually - there is no shortcut unless you write a function to do so like this:
function updatePosition(obj, leftPos, topPos) {
obj.left = leftPos;
obj.top = topPos;
}
and then call that function like this:
updatePosition(FETypes.textareaTemplate, someVariable1, someVariable2);
FEtypes
does not refer to an Array
instance. As a result, it has no 1
property, and undefined
, which is the result of FEtypes[1]
has no properties at all. Your code does not, cannot work. Check the error console.
try this using lodash or underscore:
_.chain(FEtypes).map(item => {
item.left = item.type === "Text" ? someVariable1 : item.left;
item.top = item.type === "Text" ? someVariable2 : item.top;
return item;
});
in modern javascript
Object.assign(FEtypes[1], {
left: someVariable1,
top: someVariable2
})
// FEtypes[1].left = someVariable1;
// FEtypes[1].top = someVariable2;