1

i have a data (array of object)

example data.

"data": [
{
"name": "name",
"mockupImages": "http://test.com/image1.png,http://test.com/image2.png"
}]

=========================================== here's my code snippets

<Row>
<Col xs={4}>
<img style={{ width: '40', height: '60'}} src={_.map(data, 'mockupImages')} />
</Col>
</Row

I already get the data mockupImages, the problem is I need to accept only one string

4
  • Do you want to show all images or just the 1st one? Commented Jun 28, 2017 at 6:22
  • I think what you want is to map all the 'mockupImages' into col If that is the case then use the function instead _.map(array, function(){...}). Then you can return a img with src=mockupImages. Commented Jun 28, 2017 at 6:25
  • just one only :) Commented Jun 28, 2017 at 6:34
  • Just for fun: with destructuring: const [result] = (([{mockupImages:u}]) => u.split(','))(data); Commented Jun 28, 2017 at 7:29

1 Answer 1

1

To get the 1st string, you can use _.get():

const data = [{"name":"name1","mockupImages":"http://test.com/image1.png"},{"name":"name2","mockupImages":"http://test.com/image2.png"}];

const result = _.get(_.get(_.map(data, 'mockupImages'), 0).split(','), 0);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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

3 Comments

I already try .get(.map(data, 'mockupImages'), 0) , but i need to get only 1 string,
You have 2 strings inside mockupImages - test.com/image1.png,http://test.com/image2.png. Add this info to your question.
I've updated the answer. Try to get the server to return something more sensible.

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.