将对象属性转换为对象数组

时间:2022-09-25 09:15:04

I'm getting output that looks like this:

我得到的输出看起来像这样:

{'1536135941922': 'true',
 '1536135962942': 'false',
 '1536135986966': 'false',
 '1536135989968': 'true'}

and I need it to look like this:

我需要它看起来像这样:

[{'1536135941922': 'true'},
 {'1536135962942': 'false'},
 {'1536135986966': 'false'},
 {'1536135989968': 'true'}]

So my front can consume it. What is the way that I can convert it?

所以我的前线可以消耗它。我可以用它转换它的方式是什么?

4 个解决方案

#1


1  

You could map the entries of the object by using new objects.

您可以使用新对象映射对象的条目。

Methods:

var object = { '1536135941922': 'true', '1536135962942': 'false', '1536135986966': 'false', '1536135989968': 'true' },
    array = Object.entries(object).map(([k, v]) => ({ [k]: v }));
  
console.log(array);

#2


1  

This is a one-liner with Object.entries and Array.map:

这是一个包含Object.entries和Array.map的单行程序:

const data = {'1536135941922': 'true',
  '1536135962942': 'false',
  '1536135986966': 'false',
  '1536135989968': 'true'};
const convert = (object) => Object.entries(data).map(([key, value]) => ({ [key]: value }));
console.log(convert(data));

What is your backend set-up? I was assuming Node since you tagged with javascript.

你的后端设置是什么?因为你用javascript标记我正在假设Node。

#3


1  

You can use Object.entries() and .map() methods to get the desired output:

您可以使用Object.entries()和.map()方法来获取所需的输出:

let data = {
  '1536135941922': 'true',
  '1536135962942': 'false',
  '1536135986966': 'false',
  '1536135989968': 'true'
};
  
let result = Object.entries(data).map(( [k, v] ) => ({ [k]: v }));

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

#4


0  

If you are using PHP you can create object for object and push them to an array. After that, you can convert the array object to json with json_encode functions and return it to your front.

如果您使用的是PHP,则可以为对象创建对象并将其推送到数组。之后,您可以使用json_encode函数将数组对象转换为json并将其返回到您的前端。

#1


1  

You could map the entries of the object by using new objects.

您可以使用新对象映射对象的条目。

Methods:

var object = { '1536135941922': 'true', '1536135962942': 'false', '1536135986966': 'false', '1536135989968': 'true' },
    array = Object.entries(object).map(([k, v]) => ({ [k]: v }));
  
console.log(array);

#2


1  

This is a one-liner with Object.entries and Array.map:

这是一个包含Object.entries和Array.map的单行程序:

const data = {'1536135941922': 'true',
  '1536135962942': 'false',
  '1536135986966': 'false',
  '1536135989968': 'true'};
const convert = (object) => Object.entries(data).map(([key, value]) => ({ [key]: value }));
console.log(convert(data));

What is your backend set-up? I was assuming Node since you tagged with javascript.

你的后端设置是什么?因为你用javascript标记我正在假设Node。

#3


1  

You can use Object.entries() and .map() methods to get the desired output:

您可以使用Object.entries()和.map()方法来获取所需的输出:

let data = {
  '1536135941922': 'true',
  '1536135962942': 'false',
  '1536135986966': 'false',
  '1536135989968': 'true'
};
  
let result = Object.entries(data).map(( [k, v] ) => ({ [k]: v }));

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

#4


0  

If you are using PHP you can create object for object and push them to an array. After that, you can convert the array object to json with json_encode functions and return it to your front.

如果您使用的是PHP,则可以为对象创建对象并将其推送到数组。之后,您可以使用json_encode函数将数组对象转换为json并将其返回到您的前端。