0

我知道如何像这样在 Jsonnet 中进行数组理解,使用 for 循环,数组中的一个对象使用我定义的所有值循环:

local values = [
  'foo',
  'bar',
  'foobar',
];
{
list: 'movies', 
items: [
  {
      title: 'title',
      language: 'english',
      metadata: {
        author: 'god',
        value: v,
      },
    },
    for v in values
  ],
}

输出:

{
   "items": [
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "foo"
         },
         "title": "title"
      },
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "bar"
         },
         "title": "title"
      },
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "foobar"
         },
         "title": "title"
      }
   ],
   "list": "movies"
}

我现在想对数组中的多个对象执行此操作,如下所示:

local values = [
  'foor',
  'bar',
  'foorbar',
];
{
list: 'movies', 
items: [
  {
      title: 'title',
      language: 'english',
      metadata: {
        author: 'god',
        value: v,
      },
    },
    {
      title: 'title2',
      language: 'german',
      metadata: {
        author: 'devil',
        value: v,
      },
    },
    {
      title: 'title3',
      language: 'spanish',
      metadata: {
        author: 'chutulu',
        value: v,
      },
    },
  ],
}

我需要做什么才能获得以下输出?所以三个对象中的每一个都得到三个值

{
   "items": [
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "foor"
         },
         "title": "title1"
      },
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "bar"
         },
         "title": "title1"
      },
      {
         "language": "english",
         "metadata": {
            "author": "god",
            "value": "foorbar"
         },
         "title": "title1"
      }
      {
         "language": "german",
         "metadata": {
            "author": "devil",
            "value": "foor"
         },
         "title": "title2"
      },
      {
         "language": "german",
         "metadata": {
            "author": "devil",
            "value": "bar"
         },
         "title": "title2"
      },
      {
         "language": "german",
         "metadata": {
            "author": "devil",
            "value": "foorbar"
         },
         "title": "title2"
      }
      {
         "language": "spanish",
         "metadata": {
            "author": "chutulu",
            "value": "foor"
         },
         "title": "title3"
      },
      {
         "language": "spanish",
         "metadata": {
            "author": "chutulu",
            "value": "bar"
         },
         "title": "title3"
      },
      {
         "language": "spanish",
         "metadata": {
            "author": "chutulu",
            "value": "foorbar"
         },
         "title": "title3"
      }
   ],
   "list": "movies"
}
4

1 回答 1

0

粘贴以下两种可能的解决方案,并有自己的权衡(取决于您需要进一步定制最终items数组):

1)混合数组,在理解中具有值“放置”

local values = [
  'foor',
  'bar',
  'foorbar',
];
local movies = [
  {
    title: 'title',
    language: 'english',
    metadata: {
      author: 'god',
    },
  },
  {
    title: 'title2',
    language: 'german',
    metadata: {
      author: 'devil',
    },
  },
  {
    title: 'title3',
    language: 'spanish',
    metadata: {
      author: 'chutulu',
    },
  },
];

// Combine the `values` and `movies` arrays into `items`,
// overloading movie's metadata.value with each value,
// note the `metadata+:` syntax.
{
  list: 'movies',
  items: [
    movie { metadata+: { value: value } }
    for movie in movies
    for value in values
  ],
}

2) 创建movies(v)输出 3-lang 数组的函数,聚合values并展平它

local values = [
  'foor',
  'bar',
  'foorbar',
];
local movies(v) = [
  {
    title: 'title',
    language: 'english',
    metadata: {
      author: 'god',
      value: v,
    },
  },
  {
    title: 'title2',
    language: 'german',
    metadata: {
      author: 'devil',
      value: v,
    },
  },
  {
    title: 'title3',
    language: 'spanish',
    metadata: {
      author: 'chutulu',
      value: v,
    },
  },
];

// To build the final `items` array we need to flatten
// the array-of-arrays created by the comprehension, as
// each element is indeed a 3-movies array
{
  list: 'movies',
  items: std.flattenArrays([
    movies(v)
    for v in values
  ]),
}
于 2022-02-16T12:07:16.273 回答