如何使用javascript在逗号分隔的字符串中转换json值 [英] how to convert json values in comma separated string using javascript

查看:45
本文介绍了如何使用javascript在逗号分隔的字符串中转换json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON字符串:

I have following JSON string :

{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}

我希望location_id为

I want location_id as

3,2

推荐答案

简单:

var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
  return val.location_id;
}).join(',');

console.log(result)

我假设你想要一个字符串,因此 .join(','),如果你想要一个数组只需删除它部分。

I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.

这篇关于如何使用javascript在逗号分隔的字符串中转换json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆