1

user.js

import React, { useState, useEffect } from 'react';
import Axios from 'axios';

const RecommendationDetail = () => {
  const [image, setImage] = useState([]);

  useEffect(() => {
    loadRekko();
}, []);


  const loadRekko = async () => {
    const res = await Axios.get(`http://localhost:3001/dashboard-rekko/${id}`,{
        headers:headers
    }); 


    console.log(res.data.response);
    var array = [];
    let a = (res.data.response.rekkoRecords.product_img)
    array.push(a)
    setImage(array)    ====>>> How can i make array like i want
    console.log(image)
    setRecommendation(res.data.response.rekkoRecords)
  }

 return (
    {image.map((it) => {
            return (
                <div key={it}>
                    <img src= {'http://localhost:3001/'+it} />
                </div>
            )
            
        })}
 )
}

Everything is working but I want to show multiple images. I am getting a response in "uploads\classpamplate.png,uploads\classpamplate1.jpg" this format and after pushing it in the array it becomes ["uploads\classpamplate.png,uploads\classpamplate1.jpg"] but what I want is ["uploads\classpamplate.png","uploads\classpamplate1.jpg"] both are separated so that I can show it using .map() function. Any help will be appreciated

1

4 Answers 4

2

You can use .split() to separate that string by comma ,:

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.

let a = (res.data.response.rekkoRecords.product_img)
setImage(a.split(','))

See a live example below:

const data = `uploads\classpamplate.png,uploads\classpamplate1.jpg`
const result = data.split(',')
console.log(result)

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

Comments

2

Just split the string by ,. it will return a new array

let result = (res.data.response.rekkoRecords.product_img).split(',');
let array = [...result];

Comments

2

You can split them using , so that it'll be converted to an array

let a = (res.data.response.rekkoRecords.product_img).split(",");
array.push(...a)

or

Directly you can call setImage like below

setImage(res.data.response.rekkoRecords.product_img.split(","));

Check if the response is empty or not and then split

let a = (res.data.response.rekkoRecords.product_img);
a = a && a.split(",") || []
setImage([...a])

2 Comments

How can I put check if there will be no image it will not show anything as if I have one image then it has a length as 1 and if there is no image then also it has a length 1
I have updated the answer to check if the received response is empty or not and then splitting. Please check.
1

I am assuming a = "uploads\classpamplate.png,uploads\classpamplate1.jpg"

expected result = ["uploads\classpamplate.png","uploads\classpamplate1.jpg"]

Don't push a in the array, try below

setImage(a.split(','))

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.