嗨,我的应用程序中有以下数组。我想根据这个数组制作三个列表。
[ 'type addPet {\n # default field\n empty: String\n}',
'type createUser {\n # default field\n empty: String\n}',
'type createUsersWithArrayInput {\n # default field\n empty: String\n}',
'type createUsersWithListInput {\n # default field\n empty: String\n}',
'type deleteOrder {\n # default field\n empty: String\n}',
'type deletePet {\n # default field\n empty: String\n}',
'type deleteUser {\n # default field\n empty: String\n}',
'type findPetsByStatus_items {\n id: String\n category: findPetsByStatus_items_category\n name: String\n photoUrls: [String]\n tags: [findPetsByStatus_items_tags_items]\n\n # pet status in the store\n status: String\n}',
'type findPetsByStatus_items_category {\n id: String\n name: String\n}',
'type findPetsByStatus_items_tags_items {\n id: String\n name: String\n}',
'type findPetsByTags_items {\n id: String\n category: findPetsByTags_items_category\n name: String\n photoUrls: [String]\n tags: [findPetsByTags_items_tags_items]\n\n # pet status in the store\n status: String\n}',
'type findPetsByTags_items_category {\n id: String\n name: String\n}',
'type findPetsByTags_items_tags_items {\n id: String\n name: String\n}',
'type getInventory {\n # default field\n empty: String\n}',
'type getOrderById {\n id: String\n petId: String\n quantity: Int\n shipDate: String\n\n # Order Status\n status: String\n complete: Boolean\n}',
'type getPetById {\n id: String\n category: getPetById_category\n name: String\n photoUrls: [String]\n tags: [getPetById_tags_items]\n\n # pet status in the store\n status: String\n}',
'type getPetById_category {\n id: String\n name: String\n}',
'type getPetById_tags_items {\n id: String\n name: String\n}',
'type getUserByName {\n id: String\n username: String\n firstName: String\n lastName: String\n email: String\n password: String\n phone: String\n\n # User Status\n userStatus: Int\n}',
'type loginUser {\n # default field\n empty: String\n}',
'type logoutUser {\n # default field\n empty: String\n}',
'type Mutation {\n addPet(body: param_addPet_body): addPet\n updatePet(body: param_updatePet_body): updatePet\n updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm\n deletePet(api_key: String, petId: String): deletePet\n placeOrder(body: param_placeOrder_body): placeOrder\n\n # For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors\n deleteOrder(orderId: String): deleteOrder\n\n # This can only be done by the logged in user.\n createUser(body: param_createUser_body): createUser\n createUsersWithArrayInput(body: param_createUsersWithArrayInput_body): createUsersWithArrayInput\n createUsersWithListInput(body: param_createUsersWithListInput_body): createUsersWithListInput\n\n # This can only be done by the logged in user.\n updateUser(username: String, body: param_updateUser_body): updateUser\n\n # This can only be done by the logged in user.\n deleteUser(username: String): deleteUser\n}',
'# Pet object that needs to be added to the store\ninput param_addPet_body {\n # default field\n empty: String\n}',
'# Created user object\ninput param_createUser_body {\n # default field\n empty: String\n}',
'# List of user object\ninput param_createUsersWithArrayInput_body {\n # default field\n empty: String\n}',
'# List of user object\ninput param_createUsersWithListInput_body {\n # default field\n empty: String\n}',
'# order placed for purchasing the pet\ninput param_placeOrder_body {\n # default field\n empty: String\n}',
'# Pet object that needs to be added to the store\ninput param_updatePet_body {\n # default field\n empty: String\n}',
'# Updated user object\ninput param_updateUser_body {\n # default field\n empty: String\n}',
'type placeOrder {\n id: String\n petId: String\n quantity: Int\n shipDate: String\n\n # Order Status\n status: String\n complete: Boolean\n}',
'type Query {\n viewer: viewer\n}',
'type updatePet {\n # default field\n empty: String\n}',
'type updatePetWithForm {\n # default field\n empty: String\n}',
'type updateUser {\n # default field\n empty: String\n}',
'type viewer {\n # Multiple status values can be provided with comma separated strings\n findPetsByStatus(status: [String]): [findPetsByStatus_items]\n\n # Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.\n findPetsByTags(tags: [String]): [findPetsByTags_items]\n\n # Returns a single pet\n getPetById(petId: String): getPetById\n\n # Returns a map of status codes to quantities\n getInventory: getInventory\n\n # For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions\n getOrderById(orderId: String): getOrderById\n loginUser(username: String, password: String): loginUser\n logoutUser: logoutUser\n getUserByName(username: String): getUserByName\n}' ]
在这个数组中,我想制作三个列表。一个列表包含
type Mutation {\n addPet(body: param_addPet_body): addPet\n updatePet(body: param_updatePet_body): updatePet\n updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm\n deletePet(api_key: String, petId: String): deletePet\n placeOrder(body: param_placeOrder_body): placeOrder\n\n # For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors\n deleteOrder(orderId: String): deleteOrder\n\n # This can only be done by the logged in user.\n createUser(body: param_createUser_body): createUser\n createUsersWithArrayInput(body: param_createUsersWithArrayInput_body): createUsersWithArrayInput\n createUsersWithListInput(body: param_createUsersWithListInput_body): createUsersWithListInput\n\n # This can only be done by the logged in user.\n updateUser(username: String, body: param_updateUser_body): updateUser\n\n # This can only be done by the logged in user.\n deleteUser(username: String): deleteUser\n}
第二个清单包含
type viewer {\n # Multiple status values can be provided with comma separated strings\n findPetsByStatus(status: [String]): [findPetsByStatus_items]\n\n # Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.\n findPetsByTags(tags: [String]): [findPetsByTags_items]\n\n # Returns a single pet\n getPetById(petId: String): getPetById\n\n # Returns a map of status codes to quantities\n getInventory: getInventory\n\n # For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions\n getOrderById(orderId: String): getOrderById\n loginUser(username: String, password: String): loginUser\n logoutUser: logoutUser\n getUserByName(username: String): getUserByName\n}
和第三个列表应该包含以下项目,应该包含上述数组的其余项目。
type Query {\n viewer: viewer\n}
type viewer {\n # Multiple status values can be provided with comma separated strings\n findPetsByStatus(status: [String]): [findPetsByStatus_items]\n\n # Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.\n findPetsByTags(tags: [String]): [findPetsByTags_items]\n\n # Returns a single pet\n getPetById(petId: String): getPetById\n\n # Returns a map of status codes to quantities\n getInventory: getInventory\n\n # For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions\n getOrderById(orderId: String): getOrderById\n loginUser(username: String, password: String): loginUser\n logoutUser: logoutUser\n getUserByName(username: String): getUserByName\n}
type Mutation {\n addPet(body: param_addPet_body): addPet\n updatePet(body: param_updatePet_body): updatePet\n updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm\n deletePet(api_key: String, petId: String): deletePet\n placeOrder(body: param_placeOrder_body): placeOrder\n\n # For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors\n deleteOrder(orderId: String): deleteOrder\n\n # This can only be done by the logged in user.\n createUser(body: param_createUser_body): createUser\n createUsersWithArrayInput(body: param_createUsersWithArrayInput_body): createUsersWithArrayInput\n createUsersWithListInput(body: param_createUsersWithListInput_body): createUsersWithListInput\n\n # This can only be done by the logged in user.\n updateUser(username: String, body: param_updateUser_body): updateUser\n\n # This can only be done by the logged in user.\n deleteUser(username: String): deleteUser\n}
有没有人知道如何在javascript中执行此操作?
答案 0 :(得分:1)
使用Array#reduce迭代数组。使用正则表达式获取类型字符串。从Map r
获取相关数组,然后推送到数组。返回r
(累加器)。完成reduce后,使用Map#values和spread获取数组数组,使用destructure分隔变量。
const data = ["type addPet {\n # default field\n empty: String\n}","type createUser {\n # default field\n empty: String\n}","type createUsersWithArrayInput {\n # default field\n empty: String\n}","type createUsersWithListInput {\n # default field\n empty: String\n}","type deleteOrder {\n # default field\n empty: String\n}","type deletePet {\n # default field\n empty: String\n}","type deleteUser {\n # default field\n empty: String\n}","type findPetsByStatus_items {\n id: String\n category: findPetsByStatus_items_category\n name: String\n photoUrls: [String]\n tags: [findPetsByStatus_items_tags_items]\n\n # pet status in the store\n status: String\n}","type findPetsByStatus_items_category {\n id: String\n name: String\n}","type findPetsByStatus_items_tags_items {\n id: String\n name: String\n}","type findPetsByTags_items {\n id: String\n category: findPetsByTags_items_category\n name: String\n photoUrls: [String]\n tags: [findPetsByTags_items_tags_items]\n\n # pet status in the store\n status: String\n}","type findPetsByTags_items_category {\n id: String\n name: String\n}","type findPetsByTags_items_tags_items {\n id: String\n name: String\n}","type getInventory {\n # default field\n empty: String\n}","type getOrderById {\n id: String\n petId: String\n quantity: Int\n shipDate: String\n\n # Order Status\n status: String\n complete: Boolean\n}","type getPetById {\n id: String\n category: getPetById_category\n name: String\n photoUrls: [String]\n tags: [getPetById_tags_items]\n\n # pet status in the store\n status: String\n}","type getPetById_category {\n id: String\n name: String\n}","type getPetById_tags_items {\n id: String\n name: String\n}","type getUserByName {\n id: String\n username: String\n firstName: String\n lastName: String\n email: String\n password: String\n phone: String\n\n # User Status\n userStatus: Int\n}","type loginUser {\n # default field\n empty: String\n}","type logoutUser {\n # default field\n empty: String\n}","type Mutation {\n addPet(body: param_addPet_body): addPet\n updatePet(body: param_updatePet_body): updatePet\n updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm\n deletePet(api_key: String, petId: String): deletePet\n placeOrder(body: param_placeOrder_body): placeOrder\n\n # For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors\n deleteOrder(orderId: String): deleteOrder\n\n # This can only be done by the logged in user.\n createUser(body: param_createUser_body): createUser\n createUsersWithArrayInput(body: param_createUsersWithArrayInput_body): createUsersWithArrayInput\n createUsersWithListInput(body: param_createUsersWithListInput_body): createUsersWithListInput\n\n # This can only be done by the logged in user.\n updateUser(username: String, body: param_updateUser_body): updateUser\n\n # This can only be done by the logged in user.\n deleteUser(username: String): deleteUser\n}","# Pet object that needs to be added to the store\ninput param_addPet_body {\n # default field\n empty: String\n}","# Created user object\ninput param_createUser_body {\n # default field\n empty: String\n}","# List of user object\ninput param_createUsersWithArrayInput_body {\n # default field\n empty: String\n}","# List of user object\ninput param_createUsersWithListInput_body {\n # default field\n empty: String\n}","# order placed for purchasing the pet\ninput param_placeOrder_body {\n # default field\n empty: String\n}","# Pet object that needs to be added to the store\ninput param_updatePet_body {\n # default field\n empty: String\n}","# Updated user object\ninput param_updateUser_body {\n # default field\n empty: String\n}","type placeOrder {\n id: String\n petId: String\n quantity: Int\n shipDate: String\n\n # Order Status\n status: String\n complete: Boolean\n}","type Query {\n viewer: viewer\n}","type updatePet {\n # default field\n empty: String\n}","type updatePetWithForm {\n # default field\n empty: String\n}","type updateUser {\n # default field\n empty: String\n}","type viewer {\n # Multiple status values can be provided with comma separated strings\n findPetsByStatus(status: [String]): [findPetsByStatus_items]\n\n # Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.\n findPetsByTags(tags: [String]): [findPetsByTags_items]\n\n # Returns a single pet\n getPetById(petId: String): getPetById\n\n # Returns a map of status codes to quantities\n getInventory: getInventory\n\n # For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions\n getOrderById(orderId: String): getOrderById\n loginUser(username: String, password: String): loginUser\n logoutUser: logoutUser\n getUserByName(username: String): getUserByName\n}"];
const [arr1, arr2, arr3] = [...data.reduce((r, row) => {
const [typeStr] = row.match(/^type\s[^\s]+/i) || [];
const subArr = r.has(typeStr) ? r.get(typeStr) : r.get('other');
subArr.push(row);
return r;
}, new Map([['type Mutation', []], ['type viewer', []], ['other', []]])).values()];
console.log(arr1);
console.log(arr2);
console.log(arr3);
答案 1 :(得分:0)
代码很简单......或者我没有得到请求
const array = [...]
const list0 = [], list1 = [], list2 = []
for(const row of array) {
if(row.indexOf('type Mutation') === 0)
list0.push(row)
else if(row.indexOf('type viewer') === 0)
list1.push(row)
else if(row.indexOf('type Query') === -1)
list2.push(row)
}
答案 2 :(得分:0)
如果您希望通过文本内容进行操作,可以使用javascript中的数组过滤功能,并匹配文本出现(不区分大小写)。这是一个非常基本的例子(未经测试):
var list1 = GetMatchingEntries("type Mutation");
function GetMatchingEntries(matchingText){
return yourArray.filter(function(textEntry){
return textEntry.toUpperCase().indexOf(matchingText.toUpperCase()) !== -1;
});
}