3

I need some help in manipulating a value pair array to return a string in a specific layout This is the string i am trying to achieve:

'&test=id:1111|na:shoes|ca:shoe&test=id:2222|na:top|ca:tops'

This is the array I am trying to manipulate into my string

var prodlist = 
[
    {
        name: 'shoe',
        sku: '1111',
        category: 'shoes'
    },
    {
        name: 'top',
        sku: '2222',
        category: 'tops'
    }
]

Here is what I have tried.

I added the 'deleteme' into the array thinking i could to a substitute later down the script.

function(){ 
    var prods = prodlist;
    var array = [];
    for (var i = 0; i < prods.length; i++) {
        var sku = (prods[i]['sku']);
        var name = (prods[i]['name']);
        var cat = (prods[i]['category']);

        array.push({
            deleteme: &test=id,
            id: sku,
            na: name,
            ca: cat,
        });
    }
    var newarray = array.toString();
    return newarray;
}

At the moment this function returns '[object Object],[object Object]'

any help would be much appreciated.

Thanks

2
  • 1
    You are pushing objects into the array and calling the toString method of the array. That will also call the objects' toString which returns "[object Object]". You should be pushing strings into the array, not objects, e.g. array.push('&test=id' + sku + '|na:' + name + ...). Then use join.('') instead of toString or the strings will be joined with the default comma. Commented Aug 26, 2016 at 1:04
  • @RobG You're exactly right... That was perfect! Thanks so much Commented Aug 26, 2016 at 1:22

2 Answers 2

2

Quick and easy

function prods() {
     var prods = prodlist;
     var array = [];
     for (let product of prods)
        array.push('test=id:' + product.sku+ '|na:' + product.name + '|ca:' + product.category);
     return '&' + array.join('&');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why not put the '&' directly in the string that you .push()? (And then join(''))
@nnnnnn Good point, that would work perfectly. Performance wise it doesn't really matter to my knowledge.
Yeah, it doesn't actually matter, it would just make for a slightly neater return statement.
1

how about something like this?

var prodlist = 
[{name: 'shoe',
sku: '1111',
 category: 'shoes'},
 {name: 'top',
 sku: '2222',
 category: 'tops'}]

var strTemplate = "&test=id:%sku%|na:%name%|ca:%category%"

prodlist.map( function(obj){
   return strTemplate.replace(/%(\w+)%/g, function($1, $2) {
     return obj[$2]
   })
}).join('')

//returns
// "&test=id:1111|na:shoe|ca:shoes&test=id:2222|na:top|ca:tops"

or ES6 version (edited it down further as suggested by nnnnnn )

prodlist.map( obj => strTemplate.replace(/%(\w+)%/g, ($1, $2) => obj[$2])).join('')

3 Comments

Or compact ES6 (without the return and {}): prodlist.map( obj => strTemplate.replace(/%(\w+)%/g, ($1, $2) => obj[$2])).join('').
u are correct, I was in a rush.. let me correct it. good catch! :-)
P.S.: I forgot to say nice use of .replace(). +1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.