Search object key name and use function
I have an object which I am trying to push in an array
$scope.xslLetterSpacing = {
"_name": "letter-spacing",
"__prefix": "xsl",
"__text": "2pt"
};
So above the object which I am trying to push in another array:-
$scope.frontObjct = {
"stylesheet": {
"attribute-set": [{
"attribute": {
"_name": "text-align",
"__prefix": "xsl"
},
"_name": "__frontmatter",
"__prefix": "xsl"
},
{
"attribute": [{
"_name": "space-before",
"__prefix": "xsl"
},
{
"_name": "space-before.conditionality",
"__prefix": "xsl",
"__text": "retain"
},
{
"_name": "font-size",
"__prefix": "xsl",
"__text": "16pt"
},
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "500"
},
{
"_name": "line-height",
"__prefix": "xsl",
"__text": "90%"
}
],
"_name": "__frontmatter__title",
"_use-attribute-sets": "common.title",
"__prefix": "xsl"
}
],
"_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
"_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
"_version": "2.0",
"__prefix": "xsl"
}
}
So now what I am trying to do is to put condition. When _name": "letter-spacing"
is not found in the attribute-set[1]
, then push the xslLetterSpacing
object in the attribute array and if the data object with _name": "letter-spacing"
is already there in the Obj
then do nothing.
I tried this to do with it is getting repeated with number of object already present in the array.
angular.forEach($scope.frontObjct.stylesheet["attribute-set"][1].attribute , function(value, key) {
if (key !== "_name") {
//pushing extra data to attribute field
$timeout(function(){
$scope.frontObjct.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
console.log($scope.frontObjct );
},500);
}
});
Where am I going wrong ?
javascript arrays angularjs
|
show 3 more comments
I have an object which I am trying to push in an array
$scope.xslLetterSpacing = {
"_name": "letter-spacing",
"__prefix": "xsl",
"__text": "2pt"
};
So above the object which I am trying to push in another array:-
$scope.frontObjct = {
"stylesheet": {
"attribute-set": [{
"attribute": {
"_name": "text-align",
"__prefix": "xsl"
},
"_name": "__frontmatter",
"__prefix": "xsl"
},
{
"attribute": [{
"_name": "space-before",
"__prefix": "xsl"
},
{
"_name": "space-before.conditionality",
"__prefix": "xsl",
"__text": "retain"
},
{
"_name": "font-size",
"__prefix": "xsl",
"__text": "16pt"
},
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "500"
},
{
"_name": "line-height",
"__prefix": "xsl",
"__text": "90%"
}
],
"_name": "__frontmatter__title",
"_use-attribute-sets": "common.title",
"__prefix": "xsl"
}
],
"_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
"_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
"_version": "2.0",
"__prefix": "xsl"
}
}
So now what I am trying to do is to put condition. When _name": "letter-spacing"
is not found in the attribute-set[1]
, then push the xslLetterSpacing
object in the attribute array and if the data object with _name": "letter-spacing"
is already there in the Obj
then do nothing.
I tried this to do with it is getting repeated with number of object already present in the array.
angular.forEach($scope.frontObjct.stylesheet["attribute-set"][1].attribute , function(value, key) {
if (key !== "_name") {
//pushing extra data to attribute field
$timeout(function(){
$scope.frontObjct.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
console.log($scope.frontObjct );
},500);
}
});
Where am I going wrong ?
javascript arrays angularjs
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
@SPlatten Yes I need to match each and every object's_name
in the array but I am not getting how.
– WhoAmI
Nov 21 '18 at 10:19
1
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
No your wrong, JSON is a string i.e."{"a":"test"}"
a Javascript object is an object, i.e.{a:"test"}
– Liam
Nov 21 '18 at 10:22
|
show 3 more comments
I have an object which I am trying to push in an array
$scope.xslLetterSpacing = {
"_name": "letter-spacing",
"__prefix": "xsl",
"__text": "2pt"
};
So above the object which I am trying to push in another array:-
$scope.frontObjct = {
"stylesheet": {
"attribute-set": [{
"attribute": {
"_name": "text-align",
"__prefix": "xsl"
},
"_name": "__frontmatter",
"__prefix": "xsl"
},
{
"attribute": [{
"_name": "space-before",
"__prefix": "xsl"
},
{
"_name": "space-before.conditionality",
"__prefix": "xsl",
"__text": "retain"
},
{
"_name": "font-size",
"__prefix": "xsl",
"__text": "16pt"
},
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "500"
},
{
"_name": "line-height",
"__prefix": "xsl",
"__text": "90%"
}
],
"_name": "__frontmatter__title",
"_use-attribute-sets": "common.title",
"__prefix": "xsl"
}
],
"_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
"_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
"_version": "2.0",
"__prefix": "xsl"
}
}
So now what I am trying to do is to put condition. When _name": "letter-spacing"
is not found in the attribute-set[1]
, then push the xslLetterSpacing
object in the attribute array and if the data object with _name": "letter-spacing"
is already there in the Obj
then do nothing.
I tried this to do with it is getting repeated with number of object already present in the array.
angular.forEach($scope.frontObjct.stylesheet["attribute-set"][1].attribute , function(value, key) {
if (key !== "_name") {
//pushing extra data to attribute field
$timeout(function(){
$scope.frontObjct.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
console.log($scope.frontObjct );
},500);
}
});
Where am I going wrong ?
javascript arrays angularjs
I have an object which I am trying to push in an array
$scope.xslLetterSpacing = {
"_name": "letter-spacing",
"__prefix": "xsl",
"__text": "2pt"
};
So above the object which I am trying to push in another array:-
$scope.frontObjct = {
"stylesheet": {
"attribute-set": [{
"attribute": {
"_name": "text-align",
"__prefix": "xsl"
},
"_name": "__frontmatter",
"__prefix": "xsl"
},
{
"attribute": [{
"_name": "space-before",
"__prefix": "xsl"
},
{
"_name": "space-before.conditionality",
"__prefix": "xsl",
"__text": "retain"
},
{
"_name": "font-size",
"__prefix": "xsl",
"__text": "16pt"
},
{
"_name": "font-weight",
"__prefix": "xsl",
"__text": "500"
},
{
"_name": "line-height",
"__prefix": "xsl",
"__text": "90%"
}
],
"_name": "__frontmatter__title",
"_use-attribute-sets": "common.title",
"__prefix": "xsl"
}
],
"_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
"_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
"_version": "2.0",
"__prefix": "xsl"
}
}
So now what I am trying to do is to put condition. When _name": "letter-spacing"
is not found in the attribute-set[1]
, then push the xslLetterSpacing
object in the attribute array and if the data object with _name": "letter-spacing"
is already there in the Obj
then do nothing.
I tried this to do with it is getting repeated with number of object already present in the array.
angular.forEach($scope.frontObjct.stylesheet["attribute-set"][1].attribute , function(value, key) {
if (key !== "_name") {
//pushing extra data to attribute field
$timeout(function(){
$scope.frontObjct.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
console.log($scope.frontObjct );
},500);
}
});
Where am I going wrong ?
javascript arrays angularjs
javascript arrays angularjs
edited Nov 21 '18 at 10:28
WhoAmI
asked Nov 21 '18 at 10:09
WhoAmIWhoAmI
13211
13211
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
@SPlatten Yes I need to match each and every object's_name
in the array but I am not getting how.
– WhoAmI
Nov 21 '18 at 10:19
1
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
No your wrong, JSON is a string i.e."{"a":"test"}"
a Javascript object is an object, i.e.{a:"test"}
– Liam
Nov 21 '18 at 10:22
|
show 3 more comments
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
@SPlatten Yes I need to match each and every object's_name
in the array but I am not getting how.
– WhoAmI
Nov 21 '18 at 10:19
1
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
No your wrong, JSON is a string i.e."{"a":"test"}"
a Javascript object is an object, i.e.{a:"test"}
– Liam
Nov 21 '18 at 10:22
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
@SPlatten Yes I need to match each and every object's
_name
in the array but I am not getting how.– WhoAmI
Nov 21 '18 at 10:19
@SPlatten Yes I need to match each and every object's
_name
in the array but I am not getting how.– WhoAmI
Nov 21 '18 at 10:19
1
1
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
No your wrong, JSON is a string i.e.
"{"a":"test"}"
a Javascript object is an object, i.e. {a:"test"}
– Liam
Nov 21 '18 at 10:22
No your wrong, JSON is a string i.e.
"{"a":"test"}"
a Javascript object is an object, i.e. {a:"test"}
– Liam
Nov 21 '18 at 10:22
|
show 3 more comments
1 Answer
1
active
oldest
votes
You can do like this
var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
return item._name === "letter-spacing";
})[0];
if(!itemLetterSpacing) {
$scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);
I am getting errorangular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409651%2fsearch-object-key-name-and-use-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do like this
var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
return item._name === "letter-spacing";
})[0];
if(!itemLetterSpacing) {
$scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);
I am getting errorangular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
add a comment |
You can do like this
var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
return item._name === "letter-spacing";
})[0];
if(!itemLetterSpacing) {
$scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);
I am getting errorangular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
add a comment |
You can do like this
var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
return item._name === "letter-spacing";
})[0];
if(!itemLetterSpacing) {
$scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);
You can do like this
var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
return item._name === "letter-spacing";
})[0];
if(!itemLetterSpacing) {
$scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);
edited Nov 21 '18 at 10:51
answered Nov 21 '18 at 10:39
Kipl DeveloperKipl Developer
513
513
I am getting errorangular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
add a comment |
I am getting errorangular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
I am getting error
angular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
I am getting error
angular.js:13424 Error: [ng:areq] Argument controller is not a function
– WhoAmI
Nov 21 '18 at 10:46
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
copy now and try
– Kipl Developer
Nov 21 '18 at 10:52
1
1
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
Thanks , This is exactly I was looking for.
– WhoAmI
Nov 21 '18 at 11:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409651%2fsearch-object-key-name-and-use-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
'attribute' is an array, don't you need to iterate through the array and test each object in the array?
– SPlatten
Nov 21 '18 at 10:12
@SPlatten Yes I need to match each and every object's
_name
in the array but I am not getting how.– WhoAmI
Nov 21 '18 at 10:19
1
There is no JSON in this question. Those are javascript objects, JSON is a string notation
– Liam
Nov 21 '18 at 10:19
@Liam I am pretty sure those JSON are defined in javascript objects. Isn't it ?
– WhoAmI
Nov 21 '18 at 10:21
No your wrong, JSON is a string i.e.
"{"a":"test"}"
a Javascript object is an object, i.e.{a:"test"}
– Liam
Nov 21 '18 at 10:22