简体   繁体   中英

How to create array of string with each string enclosed with single quote(javascript)

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

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 + "'");

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

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM