I have a JSON object which I get by default :-
$scope.ContentObj= {
"attribute-set": [
{
"attribute": [
{
"_name": "text-align",
"__prefix": "xsl",
"__text": "end"
},
{
"_name": "end-indent",
"__prefix": "xsl",
"__text": "10pt"
}
],
"_name": "odd__header",
"__prefix": "xsl"
},
{
"attribute": {
"_name": "font-weight",
"__prefix": "xsl",
"__text": "bold"
},
"_name": "pagenum",
"__prefix": "xsl"
}
],
"_version": "2.0",
"__prefix": "xsl"
}
NOTE:- I am doing the operation on attribute of second attribute-set name: pagenum
Now , I am making the attribute-set[1]-> attribute an array since it is an object. I am making attribute an array because I need to push more objects in it.
if(typeof $scope.ContentObj.stylesheet["attribute-set"][1].attribute === "object"){ //Check if object
const content = $scope.ContentObj.stylesheet["attribute-set"][1].attribute; //Get the content
$scope.ContentObj.stylesheet["attribute-set"][1].attribute = [content]; //Put the content in an array
}
Now it is successfully creating an array :-
$scope.ContentObj= {
"attribute-set": [
{
"attribute": [
{
"_name": "text-align",
"__prefix": "xsl",
"__text": "end"
},
{
"_name": "end-indent",
"__prefix": "xsl",
"__text": "10pt"
}
],
"_name": "odd__header",
"__prefix": "xsl"
},
{
"attribute":[
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "bold"
}
],
"_name": "pagenum",
"__prefix": "xsl"
}
],
"_version": "2.0",
"__prefix": "xsl"
}
After this I am trying to push by the objects by checking _name which is already present there or not . I am able to push this code successfully in the array by following code :-
//check color
var checkContentPageColor = obj => obj._name === 'color';
//for checking font name
var checkContentPageFont = obj => obj._name === 'font-family';
//check color in the attr json
var checkContentPageColor_available = $scope.ContentObj.stylesheet["attribute-set"][1].attribute.some(checkContentPageColor);
// check font family
var checkContentPageFont_available = $scope.ContentObj.stylesheet["attribute-set"][1].attribute.some(checkContentPageFont);
if( checkContentPageColor_available === false && checkContentPageFont_available === false ){
console.log('not available' );
$scope.ContentObj.stylesheet["attribute-set"][1].attribute.push({
"_name": "color",
"__prefix": "xsl",
"__text": "black"
},{
"_name": "font-family",
"__prefix": "xsl",
"__text": "sans"
}
);
console.log("pushed successfully");
console.log($scope.ContentObj);
}
So now , I am getting result like this { attribute: [{..},{..},{..}],something } which is correct. :-
$scope.ContentObj= {
"attribute-set": [
{
"attribute": [
{
"_name": "text-align",
"__prefix": "xsl",
"__text": "end"
},
{
"_name": "end-indent",
"__prefix": "xsl",
"__text": "10pt"
}
],
"_name": "odd__header",
"__prefix": "xsl"
},
{
"attribute":[
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "100"
},
{
"_name": "color",
"__prefix": "xsl",
"__text": "black"
},
{
"_name": "font-family",
"__prefix": "xsl",
"__text": "sans"
}
],
"_name": "pagenum",
"__prefix": "xsl"
}
],
"_version": "2.0",
"__prefix": "xsl"
}
After this when I am reloading the app , the code is getting push again making double array. The JSON now looks like this { attribute: [[{..},{..},{..}],{..},{..}],something } :-
{
"attribute-set": [
{
"attribute": [
{
"_name": "text-align",
"__prefix": "xsl",
"__text": "end"
},
{
"_name": "end-indent",
"__prefix": "xsl",
"__text": "10pt"
}
],
"_name": "odd__header",
"__prefix": "xsl"
},
{
"attribute":[[
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "100"
},
{
"_name": "color",
"__prefix": "xsl",
"__text": "black"
},
{
"_name": "font-family",
"__prefix": "xsl",
"__text": "sans"
}],
{
"_name": "color",
"__prefix": "xsl",
"__text": "black"
},
{
"_name": "font-family",
"__prefix": "xsl",
"__text": "sans"
}
],
"_name": "pagenum",
"__prefix": "xsl"
}
],
"_version": "2.0",
"__prefix": "xsl"
}
Where am I going wrong ? Suggest some changes according to my code which I should make. I am stuck here since long time.