-2

I have data that looks like this:

var data = {status: "OK", address11: 0.2, address12: 0.3}

I want this string:

Transfer Successful! You transferred: 0.2 to address11, 0.3 to address12

How do I do this efficiently?

I have this:

var a = 'Transfer Successful! You transferred:'
for (const key of Object.keys(data)) {a.concat(`${data[key]} to ${key}`)}

But it's only appending the last key/value pair.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • Strings are immutable. You need to append. – SLaks Jan 31 '18 at 19:27
  • Possible duplicate of [Converting an object to a string](https://stackoverflow.com/questions/5612787/converting-an-object-to-a-string) – Obsidian Age Jan 31 '18 at 19:27
  • 1
    You're just missing out the assignment: `a = a.concat("stuff to add");` (or `a += "stuff to add";`). `concat` returns a *new* string, it doesn't (and can't) change the string you call it on. First stop is always a good reference, such as MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat – T.J. Crowder Jan 31 '18 at 19:28

4 Answers4

1

Just use += operator in order to append a string to another.

Also , you have to remove the first key from your object. For this, you can use slice method applied for key's array.

keys = Object.keys(data).slice(1);

Use += for performance because is faster than concat.

MDN has the following to say about string.concat():

It is strongly recommended to use the string concatenation operators (+, +=) instead of this method for perfomance reasons

var data = {status: "OK", address11: 0.2, address12: 0.3}
var a = 'Transfer Successful! You transferred:';
keys = Object.keys(data).slice(1);
for (const key of keys) {a +=` ${data[key]} to ${key}`}
console.log(a);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

You need to store this a.concat('${data[key]} to ${key}' to your variable a

a = a.concat(`${data[key]} to ${key}` 

and avoid the key status

var data = {
  status: "OK",
  address11: 0.2,
  address12: 0.3
}

var a = 'Transfer Successful! You transferred: ';
for (const key of Object.keys(data)) {
  if (key === 'status') continue
  a = a.concat(`${data[key]} to ${key} `)
}

console.log(a)
Ele
  • 33,468
  • 7
  • 37
  • 75
1

I would set up your data object differently, so you don't have to rely on what the property names are in order to know if it's "status" or some transfer address. Storing the transfers in an array also guarantees their order, which may or may not be important.

var data = {
  status: "OK",
  transfers: [{
    to: "address11",
    amount: 0.2
  },{
    to: "address12",
    amount: 0.3
  }]
};


var a = "Transfer Successful! You transferred " + data.transfers.map(t => t.amount + " to " + t.to).join(", ");

console.log(a);
James
  • 20,957
  • 5
  • 26
  • 41
0

You can do it with a cycle:

var txt = 'Transfer Successful! You transferred';
var first = true;
for (var key in data) {
    if (key !== "status") {
        first ? (first = false) : (txt += ", ");
        txt += data[key] + " " + key;
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175