How to replace value in javascript or ecmascript?

106 Views Asked by At

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'
  }
]

EDITED: So here is a little update in the question I tried to add more data to s3Data with condition that if there is no All inEmp_Id then it will be pushed to result as it is or if it have All then it will be replaced by api response but not getting expected result,

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"
},
{
"Emp_Id": "1230",
"Emp_loc": 522, 
"Emp_dept": "arts", 
"Emp_sub_dept": "Finance",
"Emp_sub_dept2": "Accountant"}
];

var IDs = response.data;
var result=[];

for(var j=0;j<s3Data.length;j++){
var getEmpId = s3Data[j].Emp_Id;
if(getEmpId==='All'){
for (var i = 0; i < IDs.length; i++) {
  var id = IDs[i][0];
  s3Data.Emp_Id = id;
  result.push(s3Data)
}
}else{
    result.push(s3Data);
}
}


console.log(result); 

Expected result for edited part

[
  {
    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'
  },
  {
    Emp_Id: 1230,
    Emp_loc: 522, 
    Emp_dept: 'arts', 
    Emp_sub_dept: 'Finance',
    Emp_sub_dept2: 'Accountant'
  }
]
2

There are 2 best solutions below

4
mplungjan On BEST ANSWER

Legacy JS answer

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

var s3Data = `[{ "Emp_Id": "All", "Emp_loc": 523, "Emp_dept": "Management", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant" }, { "Emp_Id": "1230", "Emp_loc": 522, "Emp_dept": "arts", "Emp_sub_dept": "Finance", "Emp_sub_dept2": "Accountant"} ]`; // JSON string

var objects = JSON.parse(s3Data);
var resultArray = [];
var template = "";
for (var i = 0; i < objects.length; i++) {
  var id = objects[i].Emp_Id; console.log("id",id)
  var clone = JSON.parse(JSON.stringify(objects[i]));
  if (id !== "All") {
    resultArray.push(clone); // push a clone
  }  
  else if (!template) template = clone;
}  

// continue with the one without ID

for (var i = 0; i < IDs.length; i++) {
  var id = IDs[i][0];
  var obj = JSON.parse(JSON.stringify(template));  // copy
  if (obj.Emp_Id === "All") obj.Emp_Id = id;
  resultArray.push(obj)
}  

console.log(resultArray); // Object array

6
Konrad On

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);