Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何生成指定一个指定内容,随机长度的数组? #505

Open
wolfan opened this issue May 11, 2023 · 7 comments
Open

如何生成指定一个指定内容,随机长度的数组? #505

wolfan opened this issue May 11, 2023 · 7 comments

Comments

@wolfan
Copy link

wolfan commented May 11, 2023

const data= Mock.mock({
  "Array1|1-3": ["aa","bb","cc","dd","ee","ff","gg"]
})

=>  Array1: ["ee"."bb"]

想要输出一个 1~3 长度的数组,但是内容是指定的,要怎么写呐?

@RosApr
Copy link

RosApr commented May 15, 2023

试试应该满足你的需求吧

const test = 'abcdef'.split('')
  mockjs.mock({
    'array|4': [`@pick(${test})`]
  })

 mockjs.mock({
    'array|1-4': [`@pick(${test})`]
  })

@wolfan
Copy link
Author

wolfan commented May 16, 2023

可是我的原始数据是固定的数组内容吖。需要可以随机产生指定数组当中的内容。
mock在数组处理上还是差了点啊。

@RosApr
Copy link

RosApr commented May 16, 2023

const test = 'abcdef'.split('')
  mockjs.mock({
    'array|4': [`@pick(${test})`]
  })

 mockjs.mock({
    'array|1-4': [`@pick(${test})`]
  })

这个会从test数组随机挑选然后生成长度随你指定,这个不是就满足你的需求吗,还是我理解清楚你的需求

@wolfan
Copy link
Author

wolfan commented May 16, 2023

const test = 'abcdef'.split('')
  mockjs.mock({
    'array|4': [`@pick(${test})`]
  })

 mockjs.mock({
    'array|1-4': [`@pick(${test})`]
  })

这个会从test数组随机挑选然后生成长度随你指定,这个不是就满足你的需求吗,还是我理解清楚你的需求

哎还是不成,不过不是你这写法不成,而是mock不成。
原始数据里有 ["AA","Bz(1)"] 只要数组中带有'XX(x)'的数据,mock就不能了。所以还是mock在数组处理方面还是有些不能啊。

@wll8
Copy link

wll8 commented May 26, 2023

Mock.mock({
  Array1: () => Mock.Random.pick([`a`, `b`, `c(cc)`], 2, 3)
})

@bishuilantian120

This comment was marked as resolved.

@bishuilantian120
Copy link

Mock.mock({
  Array1: () => Mock.Random.pick([`a`, `b`, `c(cc)`], 2, 3)
})

perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants