我有以下二维数组输入:
var arr = [
['A', ['Sun','Moon']],
['B', ['Cat','Dog']],
['C', ['John','Peter','Zora']]
];
使用该输入,我希望将所有组合应用于 javascript 中的给定模式:
模式中的占位符格式如下: $( name )
以下是一些带有结果的示例模式:
var pattern1 = "$(A) / $(A)";
/* Expected Result
"Sun / Sun"
"Sun / Moon"
"Moon / Sun"
"Moon / Moon"
*/
var pattern2 = "$(A)--$(B)";
/* Expected Result
"Sun--Cat"
"Sun--Dog"
"Moon--Cat"
"Moon--Dog"
*/
var pattern3 = "$(C) + $(B)";
/* Expected Result
"John + Cat"
"John + Dog"
"Peter + Cat"
"Peter + Dog"
"Zora + Cat"
"Zora + Dog"
*/
var pattern4 = "$(A) - $(A) * ( $(B) + $(C) )";
/* Expected Result
"Sun - Sun * ( Cat + John )"
"Sun - Sun * ( Cat + Peter )"
"Sun - Sun * ( Cat + Zora )"
"Sun - Sun * ( Dog + John )"
"Sun - Sun * ( Dog + Peter )"
"Sun - Sun * ( Dog + Zora )"
"Sun - Moon * ( Cat + John )"
"Sun - Moon * ( Cat + Peter )"
"Sun - Moon * ( Cat + Zora )"
"Sun - Moon * ( Dog + John )"
"Sun - Moon * ( Dog + Peter )"
"Sun - Moon * ( Dog + Zora )"
"Moon - Sun * ( Cat + John )"
"Moon - Sun * ( Cat + Peter )"
"Moon - Sun * ( Cat + Zora )"
"Moon - Sun * ( Dog + John )"
"Moon - Sun * ( Dog + Peter )"
"Moon - Sun * ( Dog + Zora )"
"Moon - Moon * ( Cat + John )"
"Moon - Moon * ( Cat + Peter )"
"Moon - Moon * ( Cat + Zora )"
"Moon - Moon * ( Dog + John )"
"Moon - Moon * ( Dog + Peter )"
"Moon - Moon * ( Dog + Zora )"
*/
模式可以是任意组合(带有重复的占位符)和任意长度。
有人可以帮助我使用javascript中的算法吗?
谢谢你。