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

2
  • 1
    If it's just an object I think Object.keys(myObj) would give you what you want. Commented Jan 15, 2017 at 7:25
  • @PatrickEvans Is myObj an array... Hmm. Commented Jan 15, 2017 at 7:26

4 Answers 4

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 + "'");
Sign up to request clarification or add additional context in comments.

1 Comment

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 stackoverflow.com/questions/41623608/… As you said my + and - sign placement was wrong but not to quote positions
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]);

1 Comment

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
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]);

8 Comments

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"'
Its called Escaping characters, in this case quotes. stackoverflow.com/questions/2004168/escape-quotes-in-javascript
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...
..[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
how are you taking the array produced here and trying to set the sorting on the angular page?
|
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.