skip to Main Content

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


  1. You are passing the entire name array to the MemberValue when you need to iterate over the array and call MemberValue for each string in the name array, and stick the MemberValues results in their own array:

    class MemberValue {
      constructor(Member) {
        this.MemberName = Member['MemberName'];
        this.hierarchyName = Member['hierarchyName'];
        this.levelName = Member['levelName'];
        this.uniqueName = Member['uniqueName'];
        this.levelDepth = Member['levelDepth'];
        this.memberKind = Member['memberKind'];
      }
    }
    
    let name = ["1.K", "2.K"];
    var memberValues = [];
    
    for (var i = 0; i < name.length; i++) {
      var filterValue = new MemberValue({
        MemberName: "R.K",
        hierarchyName: "xyz",
        levelName: name[i],
        uniqueName: name[i],
        levelDepth: 1,
        memberKind: "Regular"
      });
      memberValues.push(filterValue);
    }
    
    console.log(memberValues);
    .as-console-wrapper {
      max-height: 100%!important;
    }

    If you want to do it with the .map function, you can do it like this:

    class MemberValue {
      constructor(Member) {
        this.MemberName = Member['MemberName'];
        this.hierarchyName = Member['hierarchyName'];
        this.levelName = Member['levelName'];
        this.uniqueName = Member['uniqueName'];
        this.levelDepth = Member['levelDepth'];
        this.memberKind = Member['memberKind'];
      }
    }
    
    const name = ["1.K", "2.K"];
    
    var memberValues = name.map(function(nameValue) {
      return new MemberValue({
        MemberName : "R.K",
        hierarchyName: "xyz", 
        levelName: nameValue,  
        uniqueName: nameValue,
        levelDepth:1,
        memberKind:"Regular"
      });
    });
    
    console.log(memberValues);
    .as-console-wrapper {
      max-height: 100%!important;
    }
    Login or Signup to reply.
    • I have just created class of MemberValue, to replicate your code.
    • You are directly assigning hole array to levelName and uniqueName, that’s why the value is comming hole array and not an individual value.
    • to get individual value from name, you can loop thourgh it using map(), method of array and return a new object of MemberValue class to get array of object.
    class MemberValue {
      constructor(MemberName, hierarchyName, levelName, uniqueName, levelDepth, memberKind) {
        this.MemberName = MemberName;
        this.hierarchyName = hierarchyName;
        this.levelName = levelName;
        this.uniqueName = uniqueName;
        this.levelDepth = levelDepth;
        this.memberKind = memberKind;
      }
    }
    let name = ["1.K", "2.K"];
    let filterValue = name.map(val => {
      return new MemberValue(
        "R.K",
        "xyz",
        val,
        val,
        1,
        "Regular"
      );
    })
    console.log(filterValue);
    .as-console-wrapper {
      max-height: 100%!important;
    }
    Login or Signup to reply.
  2. You can iterate over the array using any array method and substitute the name to the relevant property.

    var array = ["1.K", "2.K"];
        
    var sampleData = {
       MemberName : "R.K",
       hierarchyName: "xyz", 
       levelName: name,  
       uniqueName: name,
       levelDepth:1,
       memberKind:"Regular"
    };
    
    var result = array.map(function(itemName) {
        var data = {...sampleData}
        data.levelName = itemName;
        data.uniqueName = itemName;
        return data;
    });
    console.log(result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search