So i have a JS Object like that :
let object =
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
For iterating, i use this : for (let i = 0; i < Object.keys(object).length; i++)
Now, what i want to do is basically this :
let value;
if (data === undefined || data === "") {
value = []
} else if (data.includes(",")) {
value = data.split(",")
} else if (data.includes(".")) {
value = data.split(".")
} else {
value = data.split(" ");
}
So, i tried Object.values(entries_array).split(",")
but split is not an Object method. So if you can help me to find a solution.
Thanks in advance.
Edit: Sorry, i'll try to describe more what i want to achieve. I want with the code, to obtain my object variable changed with the same keys but with the values splitted.
For example if the value has a comma, i want to split the value. If not, do nothing.
For now, i have this :
let object =
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
const entries = Object.entries(object);
const entries_array = Object.fromEntries(entries)
let data_val;
for (let i = 0; i < Object.keys(entries_array).length; i++) {
Object.values(entries_array).forEach(val => {
if (val === undefined || val === "") {
data_val = []
} else if (val.includes(",")) {
data_val = val.split(",")
} else if (val.includes(".")) {
data_val = val.split(".")
} else {
data_val = val.split(" ");
}
});
}
return entries_array;
question from:
https://stackoverflow.com/questions/65829829/iterate-over-a-javascript-object-and-perfom-conditional-on-values 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…