Home > OS >  JS parsing array of objects with external data?
JS parsing array of objects with external data?

Time:06-08

This might sound really trivial but I don't know how what I'm trying to achieve is called and what's the best approach here. Any hints would be really helpful.

I have some variables:

const foo = true;
const bar = ["a", "b", "c"];

And then a set of items:

const itemList = [
    {
    login: 'name',
    password: bar.a,
    isActive: foo ? true : false,
  },
  {
    login: 'name2',
    password: bar.c,
    isActive: foo ? true : false,
  }
];

I need to apply the variable values to itemList so I should end up with is:

const result = [
  {
    login: 'name',
    password: 'a',
    isActive: true,
  },
  {
    login: 'name2',
    password: 'c',
    isActive: true,
  },
  {
    login: 'name3',
    password: 'b',
    isActive: true,
  }
]

Would map be the most performant option here? Note my itemList and variables are quite long.

I've tried this approach, but it seems to be returning only [true, true, true]:

const result = itemList.map((item) => (
    item.login = item.login,
    item.password = bar[bar.indexOf(item.password)],
    item.isActive = foo
));

Thanks for any help.

CodePudding user response:

I have some variables:

const foo = true;
const bar = ["a", "b", "c"];

const itemList = [{
  login: 'name',
  password: bar.a,
  isActive: foo ? true : false,
}, {
  login: 'name2',
  password: bar.c,
  isActive: foo ? true : false,
}];

In case of the above written code, the assignments of password: b.a and password: b.c both result in assigning the undefined value, due to bar being an array which does not feature any additional own properties like a, b, c etc. ...,

The code would work though in case it provides an index/map based lookup approach like this ...

const foo = true;
const bar = ["a", "b", "c"]
  // create an index/map/lookup
  // from the above provided array.
  .reduce((index, value) => {
    index[value] = value;
    return index;
  }, {});

const itemList = [{
  login: 'name',
  password: bar.a,
  isActive: !!foo,
  // the line above delivers the same result
  // but is much shorter than the original term.
  // // isActive: foo ? true : false,
}, {
  login: 'name2',
  password: bar.c,
  isActive: !!foo,
}];

console.log({ bar, itemList });
.as-console-wrapper { min-height: 100%!important; top: 0; }

... or altered even more to that ...

const foo = true;
const bar = ["a", "b", "c"];

const barValueLookup = new Map(
  // create a real `Map` instance by passing
  // the accordingly mapped bar array.
  bar.map(value => [value, value])
);
const itemList = [{
  login: 'name',
  password: barValueLookup.get('a'),
  isActive: !!foo,
}, {
  login: 'name2',
  password: barValueLookup.get('c'),
  isActive: !!foo,
}];

console.log({ lookupEntries: [...barValueLookup], itemList });
.as-console-wrapper { min-height: 100%!important; top: 0; }

In case the OP can not touch the code which declares and assigns the entire const setup, the OP needs to map over itemList in order to achieve the desired result array. But then following precondition always needs to be met ... bar items have to be aligned each by its index according to its related itemList item.

const foo = true;
const bar = ["a", "b", "c"];

const itemList = [{
  login: 'name',
  password: bar.a,
  isActive: foo ? true : false,
}, {
  login: 'name1',
  password: bar.b,
  isActive: foo ? true : false,
}, {
  login: 'name2',
  password: bar.c,
  isActive: foo ? true : false,
}];

// above code can not be touched/changed

// ... BUT ... `bar` items are aligned
// each by its index according to its
// related `itemList` item.

const result = itemList
  .map(({ password, ...itemRest }, idx) => ({
    ...itemRest,
    password: bar[idx],
  }));

console.log({ bar, itemList, result });
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

Few observations/suggestion :

  • bar is an array of primitive values. Hence, There is no properties names 'a', 'b', or 'c' available. You can access the values via index.
  • As foo contains boolean value. Hence, Instead of foo ? true : false we can directly use foo.
  • I am little confused with the password value. It is dynamic and fill as per the index from bar array or it is hard coded as name2 having password C ?

const foo = true;
const bar = ['a', 'b', 'c'];
const itemList = [
    {
    login: 'name',
    password: null,
    isActive: null,
  },
  {
    login: 'name2',
    password: null,
    isActive: null,
  }
];

const res = itemList.map(item => {
    item.password = item.login === 'name' ? bar[0] : bar[2],
    item.isActive = foo
    return item;
});

console.log(res);

CodePudding user response:

const foo = true;
const bar = ["a", "b", "c"];
const itemList = [
    {
    login: 'name',
    password: '',
    isActive: foo ? true : false,
  },
  {
    login: 'name2',
    password: '',
    isActive: foo ? true : false,
  }
];
const result = itemList.map((item,index) => ({
    login: item.login,
    password: bar[index],
    isActive: foo}
));

console.log(result)

  • Related