Skip to main content
October 24, 2024
Solved

Extending new keys to Map array

  • October 24, 2024
  • 4 replies
  • 0 views

Hello everyone,

Please have a look at below code:

local!array1: {
    a!map(id: 1, name: "Name1"),
    a!map(id: 2, name: "Name2")
  },
  local!array2: a!forEach(
    items: enumerate(10),
    expression: concat("key", tostring(fv!index))
  ),

here i want to extend array1 map key from array2, which is dynamically generated through array2 and values should be the value of id from array1.

so i am expecting the new array would be like this:

{
    a!map(
      id: 1,
      name: "Name1",
      key1: 1,
      key2: 1,
      key3: 1,
      key4: 1,
      key5: 1,
      key6: 1,
      key7: 1,
      key8: 1,
      key9: 1,
      key10: 1
    ),
    a!map(
      id: 2,
      name: "Name2",
      key1: 2,
      key2: 2,
      key3: 2,
      key4: 2,
      key5: 2,
      key6: 2,
      key7: 2,
      key8: 2,
      key9: 2,
      key10: 2
    )
  }

any easiest way to do this. Thanks in advance 

Best answer by shanmathip7466

 Hi [mention:45d1ff3fde5d4aea898326f61218c656:e9ed411860ed4f2ba0265705b8793d05] 

Is this what you are expecting? Here as you said , array 2 is dynamic and map in Array 1 takes the key from array 2 and update it with its ID.

a!localVariables(
  local!array1: {
    a!map(id: 12, name: "Name1"),
    a!map(id: 44, name: "Name2")
  },
  local!array2: {
    fullName: "john",
    address: "ABC",
    phoneNum: "876546",
    gender: "Male",
    company: "XYZ",
    dept:"IT"
  },
  local!labelNamesInArray2: a!keys(local!array2),
  local!n: length(local!labelNamesInArray2),
  a!forEach(
    items: local!array1,
    expression: a!localVariables(
      local!id: index(fv!item, "id", null),
      a!update(
        fv!item,
        local!labelNamesInArray2,
        repeat(local!n, local!id)
      )
    )
  )
)

4 replies

stefanhelzle0001
October 24, 2024

Interesting exercise ...

a!localVariables(
  local!array1: {
    a!map(id: 1, name: "Name1"),
    a!map(id: 2, name: "Name2")
  },
  a!forEach(
    items: local!array1,
    expression: a!update(
      fv!item,
      {"key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8", "key9", "key10"},
      repeat(10, fv!index)
    )
  )
)

October 24, 2024

Hi [mention:a11f77a729fb4db2af76b09948583328:e9ed411860ed4f2ba0265705b8793d05] you just hard coded the keys , in my case the number of keys can increase or decrease.

consider enumerate(n) instead enumerate(10) for array2

stefanhelzle0001
October 24, 2024

Can I win something here?

a!localVariables(
  local!array1: {
    a!map(id: 1, name: "Name1"),
    a!map(id: 2, name: "Name2")
  },
  local!n: 10,
  a!forEach(
    items: local!array1,
    expression: a!update(
      fv!item,
      apply(concat(_,_), merge(repeat(local!n, "key"), 1+enumerate(local!n))),
      repeat(local!n, fv!index)
    )
  )
)

BTW, the apply() can be replaced by a foreach().

Where is this challenge coming from?

shanmathip7466
October 24, 2024

 Hi [mention:45d1ff3fde5d4aea898326f61218c656:e9ed411860ed4f2ba0265705b8793d05] 

Is this what you are expecting? Here as you said , array 2 is dynamic and map in Array 1 takes the key from array 2 and update it with its ID.

a!localVariables(
  local!array1: {
    a!map(id: 12, name: "Name1"),
    a!map(id: 44, name: "Name2")
  },
  local!array2: {
    fullName: "john",
    address: "ABC",
    phoneNum: "876546",
    gender: "Male",
    company: "XYZ",
    dept:"IT"
  },
  local!labelNamesInArray2: a!keys(local!array2),
  local!n: length(local!labelNamesInArray2),
  a!forEach(
    items: local!array1,
    expression: a!localVariables(
      local!id: index(fv!item, "id", null),
      a!update(
        fv!item,
        local!labelNamesInArray2,
        repeat(local!n, local!id)
      )
    )
  )
)