0

I have an object which I am iterating through it's key and pushing all of its key in an array, I need to enclose each string with single quote, so for example if I have an object {"name": true, "Updated Date": false} I want to create array of string as ['"name"', '"Updated Date"'], The reason behind requiring this format is to make angular orderBy filter work with multiple predicate an each predicate having space in between like Updated Date

I had tried following code

var myObj = {"name": true, "Updated Date": false}

var myPredicates = [];

for(var prop in myObj) {
    myPredicates.push("'" + prop + "'");
}

console.log(myPredicates);

doing that the result will be ["'name'", "'Updated Date'"] but what I want to achieve is ['"name"', '"Updated Date"']

here is a plunker I am trying to make predicate from filterObject

GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19
Sohail Faruqui
  • 442
  • 11
  • 27

4 Answers4

1

The question you are asking here--which the other answers have taken at face value and given reasonable answers--is not the question you mean to ask, based on your actually AngularJS code in your plunkr. On top of that, you have a couple misconceptions about JavaScript that are causing you further confusion:

  1. Whether you have single quotes inside double quotes or double quotes inside single quotes does not matter.
  2. When you print a string to the console in a browser it always uses double quotes (try just typing both 'abc' return and "abc" return in your console directly to see this).

So as to your actual problem: rather than this...

predicate=['"+name"','"-Updated Date"']

what AngularJS requires is this...

predicate=['+"name"','-"Updated Date"']

Notice the placement of the plus and minus signs have changed. (As you likely know, the quotes are not actually needed around name because it has no embedded spaces, but it does not hurt to have them there, as your code does.)

Thus, your plunkr code, instead of this:

predicate.push("'-" + key + "'");

should use:

predicate.push("-'" + key + "'");
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
  • Agree, It was my misconception and I think all the answers are correct in their place, I have noticed my mistake yesterday and even I answered my own question about angular `orderBy` http://stackoverflow.com/questions/41623608/angular-orderby-filter-issue-with-predicate-array?noredirect=1#comment70448916_41623608 As you said my `+` and `-` sign placement was wrong but not to quote positions – Sohail Faruqui Jan 16 '17 at 08:47
0

Just add the escaped quotation marks.

var myObj = {"name": true, "Updated Date": false}

var myPredicates = [];

for(var prop in myObj) {
    myPredicates.push('"\'' + prop + '\'"');
}

console.log(myPredicates[0]);
console.log(myPredicates[1]);
GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19
  • This will add another pair of " to my string `[""name"", ""Updated Date""]`, and it is necessary to enclose `"Updated Date"` with single quote like `'"Updated Date"'` – Sohail Faruqui Jan 15 '17 at 07:46
  • Its called Escaping characters, in this case quotes. http://stackoverflow.com/questions/2004168/escape-quotes-in-javascript – GantTheWanderer Jan 15 '17 at 08:04
  • Sorry mate, but still I did not understood the relation of the link you provided with this issue, I have provided a plunker, if we try to use the solution that I created or you suggest or Jhon, non will work, the reason for example for you solution is there is no property in my data called `""name""` so sort will not work as expected, angular filter accept array of property name `["name", "city"]` and it will sort the data base on these two properties, now the reason we need to enclose the strings with single quote is space between `Updated` and `date` fo such properties... – Sohail Faruqui Jan 15 '17 at 08:38
  • ..[continuation of previous comment] which have space in between, they need to be enclosed with pair of single quote now your solution will create an string like `""Updated Date""` and there is no such property in our data and my solution creates `"'Updated Date'"` and there is no `'Updated Date'` in data so sorting wont work, sorry for long explanation, I just wanted to make you familiar to what problem exactly I am facing, I think I need to either contact angular team or write my own sorting functionality – Sohail Faruqui Jan 15 '17 at 08:39
  • how are you taking the array produced here and trying to set the sorting on the angular page? – GantTheWanderer Jan 15 '17 at 08:58
  • Also if you can provide a working plunker with a hard coded version, then people could see what the outcome is supposed to look like. – GantTheWanderer Jan 15 '17 at 19:09
  • Well yesterday I realize my mistake and it was not about how should the quotes be places but the placement of `+` and `-` signs matters and that was creating problem, here is working plunker with expected result : https://plnkr.co/edit/jIOaPPY6zR9iFR4UgrNq?p=preview – Sohail Faruqui Jan 16 '17 at 08:50
  • great I'm glad you got it running, thanks for the working link, it helps me understand angular – GantTheWanderer Jan 16 '17 at 16:12
0

Try this: (Note I only print one element because console.log(keys) seems to format the strings in the array to look differently.

var myObj = {"name": true, "Updated Date": false}

var keys = Object.keys(myObj);

for(var i = 0; i < keys.length; i++) {
    keys[i] = "'\"" + keys[i] + "\"'";
}

console.log(keys[0]);
John F.
  • 154
  • 1
  • 10
  • well it wont help, since the version in array are look like `["'"name"'", "'"Updated Date"'"]` and if we try it on the plunker I provided to sort the data, the sorting result is not correct since the result is not as expected – Sohail Faruqui Jan 15 '17 at 08:21
0

Why do not just invert double quote " with single quote '

    var myObj = {"name": true, "Updated Date": false}

    var myPredicates = [];

    for(var prop in myObj) {
        myPredicates.push('"' + prop + '"');
    }


    for (var i=0; i<myPredicates.length; i++){
        console.log (myPredicates[i])
    }

//"name"
//"Updated Date"