3

I have the following code that produces the output I need using console.log

function product() {
    var advertiserId = 1234;
    var ord = 543210;
    var productId = "id1|id2|id3|id4".split("|"); 
    var productName = "product1|product2|product3|product4".split("|"); 
    var productItemPrice = "10|20|30|40".split("|"); 

    for(i = 0; i < productId.length; i++) {
        console.log(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);
    }
}
product()

console.log result -

1234|543210|id1|product1|10
1234|543210|id2|product2|20
1234|543210|id3|product3|30
1234|543210|id4|product4|40

When changing console.log to return, only the first line is returned. return result -

1234|543210|id1|product1|10

Is there a way to return the same results as console.log?

1
  • What exactly are you wanting to do with the returned value(s)? Commented Jul 18, 2017 at 15:35

8 Answers 8

8

You would usually push the lines to an array and return the array

function product() {
    var advertiserId = 1234;
    var ord = 543210;
    var productId = "id1|id2|id3|id4".split("|"); 
    var productName = "product1|product2|product3|product4".split("|"); 
    var productItemPrice = "10|20|30|40".split("|"); 
    var ret = [];
    for(i = 0; i < productId.length; i++) {
        ret.push(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);
    }
    return ret;
}

let data = product();
console.log(data); // array holding the lines
// or join it with a newline
console.log(data.join("\n")); // lines as string

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, exactly what I needed. Thank you.
2

Push the results into an array then use the Array#join function.

function product() {
  const advertiserId = 1234;
  const ord = 543210;
  const productId = "id1|id2|id3|id4".split("|");
  const productName = "product1|product2|product3|product4".split("|");
  const productItemPrice = "10|20|30|40".split("|");

  const results = [];

  for (let i = 0; i < productId.length; i++) {
    results.push(`${advertiserId}|${ord}|${productId[i]}|${productName[i]}|${productItemPrice[i]}`);
  }

  return results;
}

const data = product();
console.log(data.join('\n'));

Comments

1

You could also use other console commands:

console.dir(data); 

Comments

0

When you return from a function, it won't continue to execute. In your case, you can store each result and return them all at once.

function product() {
    var advertiserId = 1234;
    var ord = 543210;
    var productId = "id1|id2|id3|id4".split("|"); 
    var productName = "product1|product2|product3|product4".split("|"); 
    var productItemPrice = "10|20|30|40".split("|"); 
    var results = [];
    for(i = 0; i < productId.length; i++) {
       results.push(advertiserId + "|" + ord + "|" + productId[i] + "|" + productName[i]+ "|" +productItemPrice[i]);
    }
    return results;
}

results is now an array of strings that you can do what you want with.

Comments

0

Write the output to a variable then return. using return inside the loop will exit the loop

function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|"); 
var productName = "product1|product2|product3|product4".split("|"); 
var productItemPrice = "10|20|30|40".split("|"); 

var output = "";

    for(i = 0; i < productId.length; i++) {

    output += advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i] + "\n";

    }

    return output;
}
product()

Comments

0

I think what you need is to store every result of the iteration on a variable, and then after the for loop you return it. Like

  var a;
  for loop... { 
    a += (all of your stuff)  + '\n' 
  }
  return a;

This should keep the break lines into the variable and so keep the indentation you want like console.log but into a variable.

If you need to have separated values you could store every line into a array and return the array after the for loop.

Comments

0

A return statement stops the function immediately and can only return a single value. You can add up data in the loop and then return at the end.

function product() {
    var advertiserId = 1234;
    var ord = 543210;
    var productId = "id1|id2|id3|id4".split("|"); 
    var productName = "product1|product2|product3|product4".split("|"); 
    var productItemPrice = "10|20|30|40".split("|"); 

    var result = [];
    for(i = 0; i < productId.length; i++) {

        result.push(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);



    }
    return result;
}
product()

Comments

0

Can map() the values into an array and return the array or array joined to string

function product() {
  var advertiserId = 1234;
  var ord = 543210;
  var productId = "id1|id2|id3|id4".split("|");
  var productName = "product1|product2|product3|product4".split("|");
  var productItemPrice = "10|20|30|40".split("|");

  return productId.map(function(id, idx) {
    return [
      advertiserId,
      ord,
      productId[idx],
      productName[idx],
      productItemPrice[idx]
    ].join('|')
  }).join('\n')


}

console.log(product());

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.