skip to Main Content

I need to replace All in s3data Emp_Id with the response data’s values(expected output appended), ignore 0 and 1 in response they are not required.
S3data is json data
I can’t use inbuilt function like Object.fromEntries because they are not working in apache nifi execute script. So I have to do this with foreach or with loop so that it can work in nifi.
I tried it but it didn’t work.

var response = {
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
};

var s3Data = {
    "Emp_Id": "All",
    "Emp_loc": 523,
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_sub_dept2": "Accountant"
};
var result={}
var dataName = s3Data[0].Emp_Id;
   response.data.forEach(function(elem, index) {
      if(dataName==='All'){
        result[0].Emp_Id=elem[0];
      }
      
    });

console.log(result);

EXPECTED OUTPUT:

[
  {
    Emp_Id: 123,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 124,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 446,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 617,
    Emp_loc: 523,
   Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 620,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  },
  {
    Emp_Id: 470,
    Emp_loc: 523,
    Emp_dept: 'Management',
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  }
]

2

Answers


  1. var response = {
      "status": "success",
      "data": [
        [123, 0],
        [124, 0],
        [446, 0],
        [617, 1],
        [620, 0],
        [470, 1]
      ]
    };
    
    var s3data = {
      "Emp_Id": "All",
      "Emp_loc": 523,
      "Emp_dept": "Management",
      "Emp_sub_dept": "Finance",
      "Emp_sub_dept2": "Accountant"
    };
    var result = []
    response.data.forEach(function(elem, index) {
      const obj = Object.assign({
        Emp_Id: elem[0]
      }, s3data)
      result.push(
        obj
      );
    })
    console.log(result);
    Login or Signup to reply.
  2. Here is an object keyed on ID

    You can also get the array of values using Object.values

    var response = {
        "status": "success",
        "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
    };
    
    var s3Data = { "Emp_Id": "All", "Emp_loc": 523, "Emp_dept": "Management", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant" };
    
    const {Emp_Id, ...noId} = s3Data; // remove the Emp_Id
    
    var result = response.data.reduce((acc,[id]) => (acc[id] = {id, ...noId },acc),{}); 
      
    console.log(result); // Dictionary keyed on ID
    console.log(Object.values(result)); // Just an object array
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search