如何解析数组中的真字符串和假字符串以成为布尔值



如何解析数组中的truefalse字符串以在Javascript中成为布尔值?

例如

从:

{"id":1,"dashboardId":1,"w":2,"h":2,"x":0,"y":0,"i":"n0","minW":1,"minH":1,"maxH":1000,"moved":"false","static":"false","widget":"Photo"}

自:

{"id":1,"dashboardId":1,"w":2,"h":2,"x":0,"y":0,"i":"n0","minW":1,"minH":1,"maxH":1000,"moved":false,"static":false,"widget":"Photo"}

来自movedstatic的值必须是布尔值,但它们显示为字符串。有没有办法只更改这些值?

这是我获取数组的函数:

loadData = () => {
let dashboardId = 1;
return axios
.get('api/dashboards/' + dashboardId)
.then(result => {
//@TODO Parse true and false strings to become booleans
console.log(result);
this.setState({
items: result.data,
selectedOption: '',
newCounter: originalLayouts.length
});
})
.catch(error => {
console.log(JSON.stringify(this.state.items));
console.error('error: ', error);
})
};

解析数组中的真假字符串以成为布尔值

数组中的字符串,你说?使用Array.map()进行迭代

const items = ["true", "false", "something else"]
const booleans = items.map(boolFromStringOtherwiseNull)
console.log({items, booleans}) // show result
function boolFromStringOtherwiseNull(s) {
if (s == 'true') return true
if (s == 'false') return false
return null
}

对象?使用Object.values()进行迭代

const data = {"id":1,"dashboardId":1,"w":2,"h":2,"x":0,"y":0,"i":"n0","minW":1,"minH":1,"maxH":1000,"moved":"false","static":"false","widget":"Photo"};
const booleans = Object.values(data).map(boolFromStringOtherwiseNull); // convert
console.log({data, booleans}); // show result
function boolFromStringOtherwiseNull(s) {
if (s == 'true') return true
if (s == 'false') return false
return null
}

转换布尔字符串,并保持原始对象的结构?

const data = {"id":1,"dashboardId":1,"w":2,"h":2,"x":0,"y":0,"i":"n0","minW":1,"minH":1,"maxH":1000,"moved":"false","static":"false","widget":"Photo"}
const result = Object.entries(data)
.map(boolFromStringOtherwiseNoOp)    // convert 'boolean strings' to boolean
.reduce(gatherObjectFromEntries, {}) // collect all entries into 1 object
console.log({data, result});           // result is an object where non-boolean-strings are untouched.
function boolFromStringOtherwiseNoOp([key, value]) {
if (value == 'true') return [key, true]
if (value == 'false') return [key, false]
return [key, value]
}
function gatherObjectFromEntries(accumulation, [key, value]) {
accumulation[key] = value
return accumulation
}

希望这有帮助。干杯

您可以迭代对象,并严格检查其值是否"false""true",并手动将它们解析为布尔值。

例如

function mapObjectValues (obj = {}) {
for (var key in obj) {
var val = obj[ key ];

if (val === "false") {
obj[ key ] = false;
}
else if (val === "true") {
obj[ key ] = true;
}
}
}
var tmp = {
"id": 1,
"dashboardId": 1,
"w": 2,
"h": 2,
"x": 0,
"y": 0,
"i": "n0",
"minW": 1,
"minH": 1,
"maxH": 1000,
"moved": "false",
"static": "false",
"widget": "Photo"
};
mapObjectValues(tmp);
console.log(tmp);

您可以遍历对象键,然后比较这些布尔字符串并将实际Boolean值分配给该属性:

var data = {"id":1,"dashboardId":1,"w":2,"h":2,"x":0,"y":0,"i":"n0","minW":1,"minH":1,"maxH":1000,"moved":"false","static":"false","widget":"Photo"};
Object.keys(data).forEach((key) => {
if(data[key] === 'false'){
data[key] = false;
}
if(data[key] === 'true'){
data[key] = true;
}
});
console.log(data);

你可以这样做:

var data = { "id": 1, "dashboardId": 1, "w": 2, "h": 2, "x": 0, "y": 0, "i": "n0", "minW": 1, "minH": 1, "maxH": 1000, "moved": "false", "static": "false", "widget": "Photo" };
Object.keys(data).forEach((key) => {
if (data[key] === 'false' || data[key] === 'true') {
data[key] = JSON.parse(data[key]);
}
});
console.log(data);

'

for(key in a){
a[key] == "true" ? a[key] = true : a[key] == "false" ?  a[key] = false : ''; 
}

您可以简单地遍历对象并使用三元运算符,使其简洁,where a is your original object

最新更新