I have an source object obj that looks like this and an array input
const obj = {
name: "xyz",
filter: {
and: [
{
or: [
{
and: []
}
]
}
]
}
};
const input = ["test1\name1", "test2\name2"]
I need to push objects that are formed after spiltting input by \. After splitting, using left side of the string i need to form an object like this
{ type: "type1", value: whatever the left hand value}
Same for right side value
{ type: "type2", value: whatever the right hand value}
And these objects should be pushed to innermost and in the source object.
Expected output
{
name: "xyz",
filter: {
and: [
{
or: [
{
and: [
{ type: "type1", value: "test1" },
{ type: "type2", value: "name1" },
{ type: "type1", value: "test2" },
{ type: "type2", value: "name2" }
]
}
]
}
]
}
}
Code that I tried
function processResult(input) {
return {
name: "xyz",
filter: {
and: [
{
or: [
{
and: getUpdatedValues(input)
}
]
}
]
}
};
}
// I need the getUpdateValues to be processing the each item from the input array and then sending the two objects back after splitting
function getUpdatedValues(input){
const updated = input.map(item => {
const spilt = item.split("\\");
});
}
[ or: [ and:--->[ or:this may not be valid javascript. did you mean:[ { or:instead? Also, the output says{ type: "type1", value: "type1" },--- should it actually be:{ type: "type1", value: "test1" },. The "left hand side" in the input doesn't have"type1"- it does have"test1".objis invalid, it produces javascript errors. Expected output is also invalid..split()uses double-slash (double-backslash, to be precise) and this is correct because we need to use one as an escape-character. However, the values in the array do not escape the backslash. This is an issue.