I have a default JSON file :-
{
"_name":"__tableframe__top",
"_use-attribute-sets":"common.border__top",
"__prefix":"xsl"
}
I am trying to push some value by creating an array , but I am getting my array undefined value after pushing the data
{
"_name":"__tableframe__top",
"_use-attribute-sets":"common.border__top",
"__prefix":"xsl",
"attribute":[undefined]
}
First I am checking if the object contains array or if not then create the array. And if if already an array is there then do nothing.
if(!($scope.tObj.stylesheet["attribute-set"][4].attribute instanceof Array)){
const tableframe__top_content = $scope.tObj.stylesheet["attribute-set"][4].attribute;
$scope.tObj.stylesheet["attribute-set"][4].attribute = [tableframe__top_content];
}
After this , I am checking if attribute with _name = something is already there or not in the array. If not then push.
var checkTableFrameTopColor = obj => obj._name === 'border-before-color';
var checkTableFrameTopWidth = obj => obj._name === 'border-before-width';
var checkTableFrameTopColor_available = $scope.tObj.stylesheet["attribute-set"][4].attribute.some(checkTableFrameTopColor);
var checkTableFrameTopWidth_available = $scope.tObj.stylesheet["attribute-set"][4].attribute.some(checkTableFrameTopWidth);
if( checkTableFrameTopColor_available === false && checkTableFrameTopWidth_available === false ){
$scope.tObj.stylesheet["attribute-set"][4].attribute.push({
"_name": "border-before-color",
"__prefix": "xsl",
"__text": "black"
},{
"_name": "border-before-width",
"__prefix": "xsl",
"__text": "1pt"
}
);
console.log("pushed successfully");
console.log($scope.tObj);
}
I am getting null value on array and an error TypeError: Cannot read property '_name' of undefined at checkTableFrameTopColor.
Where am I going wrong ?
EDIT:-
Like this I want to achieve-
{
"attribute":[
{"_name":"font-weight","__prefix":"xsl","__text":"bold"},
{"_name":"color","__prefix":"xsl","__text":"black"}
],
"_name":"__tableframe__top",
"_use-attribute-sets":"common.border__top",
"__prefix":"xsl"
}
"attribute": { }is an object and is present in the main json object then it will turn to array. But thereattributeis not present from begining{ "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl", "attribute":[undefined] }