分割字符串并转换为对象数组 javascript

发布于 2025-01-10 03:56:58 字数 380 浏览 0 评论 0原文

我有一个字符串数据,我需要将其转换为 javascript 中的对象数组

下面尝试了代码

var splitarr = sample.split('|');
var result = splitarr.map(e=>{
  details: e,
  values: splitarr[e]
})


var sample =  "finance=60|IT=88|sales=50"


Expected Output

[
  {"details": "finance","tag":60},
  {"details": "IT","tag":88},
  {"details": "sales","tag":50}
]

I have a string data, I need to convert to array of objects in javascript

Below the code tried

var splitarr = sample.split('|');
var result = splitarr.map(e=>{
  details: e,
  values: splitarr[e]
})


var sample =  "finance=60|IT=88|sales=50"


Expected Output

[
  {"details": "finance","tag":60},
  {"details": "IT","tag":88},
  {"details": "sales","tag":50}
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似狗非友 2025-01-17 03:56:58

您需要使用 = 进行另一次分割,请尝试:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result = splitarr.map(e=>{
  let keyVal = e.split('=');
  return { details: keyVal[0], tag:keyVal[1] }
})

console.log(result);

另外,当您从箭头函数返回对象时,您需要使用括号 () ,否则它将被视为代码块,请尝试:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result1 = splitarr.map(e=>{
  details: e
})

console.log(result1);

var result2 = splitarr.map(e=>({
  details: e
}))

console.log(result2);

You need another split by =, try:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result = splitarr.map(e=>{
  let keyVal = e.split('=');
  return { details: keyVal[0], tag:keyVal[1] }
})

console.log(result);

Also when you return object from arrow function you neeed to use brackets (), otherwise it will be considered as block of code, try:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result1 = splitarr.map(e=>{
  details: e
})

console.log(result1);

var result2 = splitarr.map(e=>({
  details: e
}))

console.log(result2);

恰似旧人归 2025-01-17 03:56:58

将字符串转换为对象的另一种方法。

const sample =  "finance=60|IT=88|sales=50";

const result = sample
  .split('|')
  .map((e) => e.split('='))
  .map(([details, tag]) => ({ details, tag }));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Another way of transforming string to object.

const sample =  "finance=60|IT=88|sales=50";

const result = sample
  .split('|')
  .map((e) => e.split('='))
  .map(([details, tag]) => ({ details, tag }));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

小…红帽 2025-01-17 03:56:58

使用 regex 提取所有对,例如。 "finance=60" 通过使用 string.match(regex) 它将返回对数组,然后使用 Array.map() 进行映射到对象数组中。

const text =  "finance=60|IT=88|sales=50";
const convertToObject = (str) => {
  const list = str.match(/\w+=\d+/g);
  return list.map(ele => {
    const [details, tag] = ele.split("=");
    return { details, tag };
  });
}
console.log(convertToObject(text));

Use regex to extract all pairs eg. "finance=60" by using string.match(regex) which will return array of pairs and then use Array.map() to map into array of objects.

const text =  "finance=60|IT=88|sales=50";
const convertToObject = (str) => {
  const list = str.match(/\w+=\d+/g);
  return list.map(ele => {
    const [details, tag] = ele.split("=");
    return { details, tag };
  });
}
console.log(convertToObject(text));

沉睡月亮 2025-01-17 03:56:58

观察:

  • 属性应该是 tag,而不是 values 属性。
  • 因为 splitter 将是一个简单的元素数组。此 splitarr[e] 将不起作用或不存在。

您可以使用 String.split() 方法来获取 detailstag 值。

工作演示:

const sample =  "finance=60|IT=88|sales=50";

const splitarr = sample.split('|');

const result = splitarr.map((e) => {
    return {
    details: e.split('=')[0],
    tag: e.split('=')[1]
  }
});

console.log(result);

Observations :

  • Instead of values property it should be tag.
  • As splitter will be a simple array of elements. This splitarr[e] will not work or not exist.

You can use String.split() method to get the details and tag value.

Working Demo :

const sample =  "finance=60|IT=88|sales=50";

const splitarr = sample.split('|');

const result = splitarr.map((e) => {
    return {
    details: e.split('=')[0],
    tag: e.split('=')[1]
  }
});

console.log(result);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文