objectStore.delete()
我希望它像这样转换:
{"1":"val1","2":"val2","3":"val3"}
几乎没有帮助,请多加注意
答案 0 :(得分:2)
您不能在一个对象中使用相同的键名。
相反,您可以执行此操作。
const origin = {"1":"val1","2":"val2","3":"val3"}
const converted = Object.entries(origin).map( ([key,value]) => ({id: key, value }) );
console.log(converted);
答案 1 :(得分:0)
您发布的内容无效。
您可能想要的是:
const object = {"1":"val1","2":"val2","3":"val3"};
console.log(Object.entries(object));
// or
console.log(Object.keys(object).map(i => ({Id: i, value: object[i]})));
答案 2 :(得分:0)
您可以在Object.entries
上使用循环。
例如像这样:
const newObjArr = [];
for(let [key, value] of Object.entries(obj)){
newObj.push({Id: key, value});
}
上面的代码将返回一个对象数组,但是我确定您可以将其修改为您的特定用例。
答案 3 :(得分:0)
DISP=SHR
结果将为const data = {"1":"val1","2":"val2","3":"val3"};
const result = Object.keys(data).map((key) => ({ id: key, value: data[key] }));
答案 4 :(得分:0)
正如所指出的,这是马鞭草。如果要进行转换,则将如下所示:
[{"Id":"1","value":"val1"},{"Id":"2","value":"val2"},{"Id":"3","value":"val3"}]
您可以创建一个将其转换的函数。
const object = {"1":"val1","2":"val2","3":"val3"};
console.log(Convert(object));
function Convert(obj){
return Object.keys(obj).map(i => ({Id: i, value: obj[i]}));
}
答案 5 :(得分:0)
您不能这样做。对象是唯一的键值对。
{"Id":"1","value":"val1","Id":"2","value":"val2","Id":"3","value":"val3"}
假设您要合并两个对象,并且如果两个对象都具有相同的键,那么它将简单地合并最后一个对象值并且只有一个键值。
答案 6 :(得分:0)
您可以将您的大对象转换为几个小对象,并将其存储在此片段所示的数组中。 (它可能会更短,但是这个详细的演示应该更容易理解。)
// Defines a single object with several properties
const originalObject = { "1" : "val1", "2" : "val2", "3" : "val3" }
// Defines an empty array where we can add small objects
const destinationArray = [];
// Object.entries gives us an array of "entries", which are length-2 arrays
const entries = Object.entries(originalObject);
// `for...of` loops through an array
for(let currentEntry of entries){
// Each "entry" is an array with two elements
const theKey = currentEntry[0]; // First element is the key
const theValue = currentEntry[1]; // Second element is the value
// Uses the two elements as values in a new object
const smallObject = { id: theKey, value: theValue };
// Adds the new object to our array
destinationArray.push(smallObject);
} // End of for loop (reiterates if there are more entries)
// Prints completed array of small objects to the browser console
console.log(destinationArray);
答案 7 :(得分:0)
const obj = {"1":"val1","2":"val2","3":"val3"}
const newObject = Object.keys(obj).map(e => {
return {ID: e , value : obj[e] }
});
console.log(newObject); // [ { ID: '1', value: 'val1' },
{ ID: '2', value: 'val2' },
{ ID: '3', value: 'val3' } ]
它将给您一个对象数组,稍后您需要将其转换为对象并展平对象:
How do I convert array of Objects into one Object in JavaScript?