下面是我的数组:
var myArray = [
{field: 'region1.mp3.california', flag: false},
{field: 'region1.mp3.marines,region1.mp3.region2.mp3.california', flag: false},
{field: 'region2.mp3.california', flag: false},
{field: 'region2.mp3.abc', flag: false},
{field: 'region3.mp3.abc', flag: false},
];
var regions = ["region1.mp3", "region2.mp3","region3.mp3"];
现在这里的数据在第二个点之后属于特定区域。
例如: region1.mp3.california,region1.mp3.marines
california and marines belongs to region1.
所以这里我试图比较区域数组和myArray的长度,并根据myarray数据和区域数组数据长度应该相等,因为myarray包含来自区域数组的所有区域数据,因此它们的长度应相等。
预期输出:true
我没有到达如何使用reduce
函数进行比较。
var myArray = [
{field: 'region1.mp3.california', flag: false},
{field: 'region1.mp3.marines,region1.mp3.region2.mp3.california', flag: false},
{field: 'region2.mp3.california', flag: false},
{field: 'region2.mp3.abc', flag: false},
{field: 'region3.mp3.abc', flag: false},
];
var regions = ["region1.mp3", "region2.mp3","region3.mp3"];
var len1 = regions.length;
var len2 = myArray.reduce(function (p, c) {
return p.concat(c.field.split(','));
}, []).length;
if(len1 == len2)
console.log(true)
else
console.log(false)

答案 0 :(得分:1)
尝试此reduce函数调用:
const MyDatePicker = ({ input, meta: { touched, error } }) => (
<div>
<DatePicker
{...input} dateFormat="DD-MM-YYYY"
selected={input.value ? moment(input.value, 'DD-MM-YYYY') : null}
/>
{touched && error && <span>{error}</span>}
</div>
);
答案 1 :(得分:1)
我首先使用Array.prototype.map()
创建一个仅包含.field.split('.')\[0\]
的新数组,其中包含region1
或'region2'或'region3'。此数组的大小与myArray
然后我使用Array.prototype.filter()
删除重复区域。此数组仅包含region1
,region2
和region3
var myArray = [
{field: 'region1.mp3.california', flag: false},
{field: 'region1.mp3.marines,region1.mp3.region2.mp3.california', flag: false},
{field: 'region2.mp3.california', flag: false},
{field: 'region2.mp3.abc', flag: false},
{field: 'region3.mp3.abc', flag: false},
];
var regions = ["region1.mp3", "region2.mp3","region3.mp3"];
var myArrayRegions = [];
myArrayRegions = myArray.map(function(o){
return o.field.split('.')[0];
}).filter(function(item, pos, self) {
return self.indexOf(item) == pos;
});
console.log(JSON.stringify(myArrayRegions));
console.log(JSON.stringify(regions));
详细代码
var myArray = [
{field: 'region1.mp3.california', flag: false},
{field: 'region1.mp3.marines,region1.mp3.region2.mp3.california', flag: false},
{field: 'region2.mp3.california', flag: false},
{field: 'region2.mp3.abc', flag: false},
{field: 'region3.mp3.abc', flag: false},
];
var regions = ["region1.mp3", "region2.mp3","region3.mp3"];
var myArrayRegions = [];
//Create a new array with each element being the result of the callback function.
myArrayRegions = myArray.map(function(o){
//o ==> {field: 'region1.mp3.california', flag: false}
//o.field ==> region1.mp3.california
//o.field.split('.') ==> ['region1','mp3','california']
//o.field.split('.')[0] ==> 'region1'
return o.field.split('.')[0];
});
console.log(JSON.stringify(myArrayRegions));
//Filter the array to remove duplicates
myArrayRegions = myArrayRegions.filter(function(item, pos, self){
return self.indexOf(item) == pos;
});
console.log(JSON.stringify(myArrayRegions));
注意强>
您只是比较两个数组的长度。 myArray
可能包含region4
,region5
和region6
,它仍会返回true
,因为这两个数组的长度均为3