gpt4 book ai didi

JavaScript 减少,无法处理对象数组

转载 作者:行者123 更新时间:2023-11-28 05:04:09 26 4
gpt4 key购买 nike

我正在尝试在对象数组上处理此规则如果存在一个或多个类型等于固定电话的对象,则返回第一个数字,否则如果不存在类型=固定电话的对象,则仅当其类型属性与移动电话不同时才返回第一个数字,如果其等于移动电话,则返回下一个数字如果可用,否则返回空白值

var phoneObject = [
{
type: 'mobile',
phone: '0123456789'
},
{
type: 'mobile',
number:'78945610'
}
{
type:'landline'
number:'15975345'
}
];

var number = phoneObject.reduce((a,b)=>{
if (a.type === 'landline' && b.type === 'landline') {
b.number = a.number;
}
else {
if (a.type !== 'landline' && b.type !== 'landline') {
if (a.type !== 'mobile') {
b.number = a.number;
} else {
if (b.number) {
return b.number;
}
else {
b.number = '';
}
}
}
}
return b.number;
});
console.log(number);

// should return the third number for this array
var phoneObject = [
{
type: 'mobile',
phone: '0123456789'
},
{
type: 'mobile',
number:'78945610'
}
{
type:'landline'
number:'15975345'
}
];


// for this one it should return the first
var phoneObject = [
{
type: 'landline',
phone: '0123456789'
},
{
type: 'mobile',
number:'78945610'
}
{
type:'landline'
number:'15975345'
}
];

// for this one it should return the second number
{
type: 'mobile',
phone: '0123456789'
},
{
type: 'mobile',
number:'78945610'
}
];

// and for this last one it should return a blank value
var phoneObject = [
{
type: 'mobile',
phone: '0123456789'
},
];

感谢您的帮助此致,莎拉

最佳答案

您可以进行一些适当的检查并保存第二个数字。仅当没有固定电话号码时才使用它。

此提案适用于 Array#reduce

function getNumber(array) {
var counter = 0,
second = '';
return array.reduce(function (r, o) {
if (r) {
return r;
}
if (o.type === 'landline') {
return o.number;
}
counter++;
if (counter === 2) {
second = o.number;
}
}, '') || second;
}

//should return the third number for this array
var phoneObject1 = [{ type: 'mobile', number: '0123456789' }, { type: 'mobile', number:'78945610' }, { type: 'landline', number:'15975345' }];

// for this one it should return the first
var phoneObject2 = [{ type: 'landline', number: '0123456789' }, { type: 'mobile', number:'78945610' }, { type:'landline', number:'15975345' }];

// for this one it should return the second number
var phoneObject3 = [{ type: 'mobile', number: '0123456789' }, { type: 'mobile', number:'78945610' }];

//and for this last one it should return a blank value
var phoneObject4 = [{ type: 'mobile', number: '0123456789' }];

console.log([phoneObject1, phoneObject2, phoneObject3, phoneObject4].map(getNumber));

与上面相同,但使用 Array#some

function getNumber(array) {
var counter = 0,
landline = '',
second = '';

array.some(function (o) {
if (o.type === 'landline') {
landline = o.number;
return true;
}
counter++;
if (counter === 2) {
second = o.number;
}
});
return landline || second;
}

//should return the third number for this array
var phoneObject1 = [{ type: 'mobile', number: '0123456789' }, { type: 'mobile', number:'78945610' }, { type: 'landline', number:'15975345' }];

// for this one it should return the first
var phoneObject2 = [{ type: 'landline', number: '0123456789' }, { type: 'mobile', number:'78945610' }, { type:'landline', number:'15975345' }];

// for this one it should return the second number
var phoneObject3 = [{ type: 'mobile', number: '0123456789' }, { type: 'mobile', number:'78945610' }];

//and for this last one it should return a blank value
var phoneObject4 = [{ type: 'mobile', number: '0123456789' }];

console.log([phoneObject1, phoneObject2, phoneObject3, phoneObject4].map(getNumber));

关于JavaScript 减少,无法处理对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com