0

I have two arrays typeArr = [1010111,23342344] infoArr={'name':'jon,'age':25}

I am expecting following

[{'name:'jone','age':25,'type':1010111,'default':'ok'},{'name:'jone','age':25,'type':23342344,'default':'nok'}]

Code :

updaterecord(infoArr,type)
{
  infoArr.type=type;
  response = calculate(age);
  if(response)
     infoArr.default = 'ok';
 else 
    infoArr.default = 'nok';
  return infoArr;
}
createRecord(infoArr,typeArr)
{
   var data = _.map(typeArr, type => {
       return updaterecord(infoArr,type);
    });
  return (data);
}

var myData = createRecord(infoArr,typeArr);

I am getting

[{'name:'jone,'age':25.'type':23342344,'default':nok},{'name:'jone,'age':25.'type':23342344,'default':nok}]

with some reason the last record updates the previous one. I have tried generating array using index var but not sure what's wrong it keep overriding the previous item.

how can I resolve this

1 Answer 1

1

You are passing the entire infoArr array to your updaterecord() function, but updaterecord() looks like it's expecting a single object. As a result it is adding those properties to the array rather than individual members of the array.

It's not really clear what is supposed to happen because typeArr has two elements and infoArr has one. Do you want to add another to infoArr or should infoArr have the same number of elements as typeArr.

Assuming it should have the same number you would need to use the index the _map gives you to send each item from infoArr:

function createRecord(infoArr,typeArr) {
   var data = _.map(typeArr, (type, i) => {
        // use infoArr[i] to send one element
       return updaterecord(infoArr[i],type);
    });
  return (data);
}

Edit:

I'm not sure how you are calculating default since it's different in your expected output, but based on one number. To get an array of objects based on infoArray you need to copy the object and add the additional properties the you want. Object.assign() is good for this:

let typeArr = [1010111,23342344]
let infoArr={'name':'jon','age':25}

function updaterecord(infoArr,type){
    var obj = Object.assign({}, infoArr)
    return Object.assign(obj, {
        type: type,
        default: infoArr.age > 25 ? 'ok' : 'nok' //or however your figuring this out
    })
}
function createRecord(infoArr,typeArr) {
    return _.map(typeArr, type => updaterecord(infoArr,type));
}

Result:

[ { name: 'jon', age: 25, type: 1010111, default: 'nok' },
  { name: 'jon', age: 25, type: 23342344, default: 'nok' } ]
Sign up to request clarification or add additional context in comments.

4 Comments

I want to create an array with two item [name,age,type,default] but value for type and default are populated based on infoArr
So you want an array with 2 items and "jone" is both items? You want a copy of the 'jone' item?
yes jone,age same for both record but type and default fields different
You added calculate(age) but no definition? And how can you expect 'default' to be 'nok' in one and 'ok' in the other if the age is the same??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.