I have an array that contains several members. Then I created a new instance of a MemberValue object in which I passed this array called "name" as a value but it passed all the values as a single string.
var name = ["1.K", "2.K"];
var filterValue = new data.MemberValue({
MemberName : "R.K",
hierarchyName: "xyz",
levelName: name,
uniqueName: name,
levelDepth:1,
memberKind:"Regular"
});
The output we are getting in the console is
0:{MemberName : "R.K",
hierarchyName: "xyz",
levelName: ["1.K", "2.K"],
uniqueName: ["1.K", "2.K"],
levelDepth:1,
memberKind:"Regular"}
But we need output like this
0:{MemberName : "R.K",
hierarchyName: "xyz",
levelName: "1.K",
uniqueName: "1.K",
levelDepth:1,
memberKind:"Regular"}
1:{MemberName : "R.K",
hierarchyName: "xyz",
levelName: "2.K",
uniqueName: "2.K",
levelDepth:1,
memberKind:"Regular"}
Can anybody help me with this how we can convert array object as object and pass them into the instance of object that I have created.
I have tried with .map function but that not works for me
3
Answers
You are passing the entire
name
array to theMemberValue
when you need to iterate over the array and callMemberValue
for each string in thename
array, and stick theMemberValues
results in their own array:If you want to do it with the
.map
function, you can do it like this:MemberValue
, to replicate your code.levelName
anduniqueName
, that’s why the value is comming hole array and not an individual value.name
, you can loop thourgh it usingmap()
, method of array and return a new object ofMemberValue
class to get array of object.You can iterate over the array using any array method and substitute the name to the relevant property.