ProLuck
ProLuck

Reputation: 375

How to join different array value from looping result in javascript

I have array in here but I still confused how can I join my all array result from looping, from json I have data like this :

{
   "getFruit": [
       {
          "id": 1,
          "name_of_fruit": Apple,
          "price": 40000,
       },
       {
          "id": 2,
          "name_of_fruit": Orange,
          "price": 45000,
       },
       {
          "id": 3,
          "name_of_fruit": Grape,
          "price": 50000,
       },
    ]
}

When I manipulation my data with for looping the result to be like this :

for (let i = 0; i < getFruit.length; i++) {
   console.log(getFruit[i]);
}

// result in console
{"id": 1,"name_of_fruit": Apple,"price": 40000}
{"id": 2,"name_of_fruit": Orange,"price": 45000}
{"id": 3,"name_of_fruit": Grape,"price": 50000}

And for the question is how to join value in field name_of_fruit, price, and make my value into the string value? I make like this and it's doesnt work :

for (let i = 0; i < getFruit.length; i++) {
   let a = getFruit[i]['name_of_fruit'];
   let b = getFruit[i]['price'];
   console.log(a, b);
}

// result in console
Apple 40000
Orange 45000
Grape 50000

// expectation result that I want
[Apple - 40000, Orange - 45000, Grape - 50000] <= this result I wanna insert into the input value in my HTML

Thanks

Upvotes: 0

Views: 57

Answers (4)

John V
John V

Reputation: 875

Try this code

let arr = [];
for (let i = 0; i < getFruit.length; i++) {
   let a = getFruit[i]['name_of_fruit'];
   let b = getFruit[i]['price'];
   arr.push(a + ' - ' + b);
}
let result = '[' + arr.join(', ') + ']';
console.log(result);

Or you can use

let result = getFruit.reduce((a, b) => [ !a ? '' : a + ', ', b.name_of_fruit, ' - ', b.price].join(''), '');
console.log(result);

Upvotes: 2

Phil
Phil

Reputation: 164902

You can map your getFruit array into an array of strings in the format you want.

Then set that into your <input> via Array.prototype.join()

const obj = {"getFruit":[{"id":1,"name_of_fruit":"Apple","price":40000},{"id":2,"name_of_fruit":"Orange","price":45000},{"id":3,"name_of_fruit":"Grape","price":50000}]}

const formatted = obj.getFruit.map(({ name_of_fruit, price }) =>
  `${name_of_fruit} - ${price}`)
  
document.querySelector("input#whatever").value = `[${formatted.join(", ")}]`
<input id="whatever" size="50">

Upvotes: 4

dandemo
dandemo

Reputation: 397

An alternative approach to the answers provided already would be to make a string and continually concatenate to the string. Like this:

var finalInfo = "[";
for (let i = 0; i < getFruit.length; i++) {
   let a = getFruit[i]['name_of_fruit'];
   let b = getFruit[i]['price'];
   if(i != 0){
   // A comma will be added to the beginning of all iterations except the first
      finalInfo += ", ";
   }
   finalInfo += a + " - " + b;
   
}
finalInfo += "]";

Upvotes: 0

aman-aman
aman-aman

Reputation: 184

Define an Object and push whatever you need inside it.

let array = {};
for (let i = 0; i < getFruit.length; i++) {
   let a = getFruit[i]['name_of_fruit'];
   let b = getFruit[i]['price'];
   array[a] = b;
}
//Now, the result would be
//{Apple: 40000, Orange: 45000, Grape: 50000}

Upvotes: 1

Related Questions