Javascript,返回匹配的第一个(子)对象

Javascript,返回匹配的第一个(子)对象,javascript,lodash,Javascript,Lodash,我有一个对象数组,我试图通过它来抓取第一个子对象,这个子对象与我给它的某个对象相匹配。所以我的对象看起来是这样的: const filteredVariants = [ { name: "Test0", images: [ { name: "test0img1", shot_type: "swatch" }] }, { name: "Test1", images: [ { name: "test1img1", sho

我有一个对象数组,我试图通过它来抓取第一个子对象,这个子对象与我给它的某个对象相匹配。所以我的对象看起来是这样的:

const filteredVariants = [
{
   name: "Test0",
   images: [
   {
    name: "test0img1",
    shot_type: "swatch"
   }]
   }, {
   name: "Test1",
   images: [
   {
    name: "test1img1",
    shot_type: "product"
   },
   {
    name: "test1img2",
    shot_type: "product"
   }]
}]
因此,我要做的是查看这个对象,特别是在
图像中,找到第一个具有
快照类型的对象:“product”
,然后返回该对象。所以,在这种情况下,我试图让它回到我身边:

{
name: "test1img1",
shot_type: "product"
}
以下是我到目前为止的情况:

 const firstProductImage = _.find(filteredVariants, _.flow(
    _.property('images'),
    _.partialRight(_.some, { shot_type: 'product' })
 ));
部分是这样的,除了返回整个父对象,当我只需要子图像对象时。我也试过运行地图和过滤器,但这些方法并没有这个干净。我想知道在这种情况下,是否有办法告诉lodash只返回与
{shot\u type:'product'}
匹配的第一个子节点


我使用的是lodash,但如果这在纯javascript中是可能的,那也太好了。谢谢

您可以迭代
filteredVariants
图像
,如果找到,则将对象分配给
结果
。返回的truthy值结束两个循环

版本包括:

var-filteredVariants=[{name:“Test0”,图片:[{name:“test0img1”,shot_类型:“swatch”},{name:“Test1”,图片:[{name:“test1img1”,shot_类型:“product”},{name:“test1img2”,shot_类型:“product”}],
结果;
filteredVariants.some(variant=>
variant.images.some(image=>image.shot\u type===“product”&&(result=image))
);

控制台日志(结果)
您可以迭代
过滤器变量
图像
,如果找到,则将对象分配给
结果
。返回的truthy值结束两个循环

版本包括:

var-filteredVariants=[{name:“Test0”,图片:[{name:“test0img1”,shot_类型:“swatch”},{name:“Test1”,图片:[{name:“test1img1”,shot_类型:“product”},{name:“test1img2”,shot_类型:“product”}],
结果;
filteredVariants.some(variant=>
variant.images.some(image=>image.shot\u type===“product”&&(result=image))
);
控制台日志(结果)
工作小提琴:


工作小提琴:

以下几点如何:

const filteredVariants=[
{
名称:“Test0”,
图像:[
{
名称:“test0img1”,
快照类型:“样本”
}]
}, {
名称:“Test1”,
图像:[
{
名称:“test1img1”,
镜头类型:“产品”
},
{
名称:“test1img2”,
镜头类型:“产品”
}]
}]
var searchObject={shot_type:'product'};
//V------------在这里实现
var foundObject=\u0.reduce(
过滤剂,
(last,next)=>last | | |查找(next.images,searchObject),
无效的
);
console.log(foundObject)

以下内容如何:

const filteredVariants=[
{
名称:“Test0”,
图像:[
{
名称:“test0img1”,
快照类型:“样本”
}]
}, {
名称:“Test1”,
图像:[
{
名称:“test1img1”,
镜头类型:“产品”
},
{
名称:“test1img2”,
镜头类型:“产品”
}]
}]
var searchObject={shot_type:'product'};
//V------------在这里实现
var foundObject=\u0.reduce(
过滤剂,
(last,next)=>last | | |查找(next.images,searchObject),
无效的
);
console.log(foundObject)

这绝对是可能的,而且在普通js中可能不会那么混乱:

const filteredVariants=
[
{
名称:“Test0”,
图像:[{name:“test0img1”,快照类型:“swatch”}]
},
{
名称:“Test1”,
图像:[
{name:“test1img1”,shot_类型:“product”},
{名称:“test1img2”,快照类型:“产品”}
]
}
]
//将对象与给定模型进行比较。
功能比较(对象、模型){
for(模型中的str)
if(对象[str]!=model[str])
返回false;
返回true;
}
//获取所有“图像”属性,对其进行迭代并返回第一个
//匹配的。如果不起作用,则返回null
函数findImage(数组、模型){
for(过滤器变量中的var i){
var obj=过滤变量[i],
图像=对象图像;
for(图像中的var j){
if(比较(图像[j],模型))
返回图像[j];
}
}
返回null;
}

log(findImage(filteredVariants,{shot_type:“product”}));
这绝对是可能的,而且在普通js中可能更容易混淆:

const filteredVariants=
[
{
名称:“Test0”,
图像:[{name:“test0img1”,快照类型:“swatch”}]
},
{
名称:“Test1”,
图像:[
{name:“test1img1”,shot_类型:“product”},
{名称:“test1img2”,快照类型:“产品”}
]
}
]
//将对象与给定模型进行比较。
功能比较(对象、模型){
for(模型中的str)
if(对象[str]!=model[str])
返回false;
返回true;
}
//获取所有“图像”属性,对其进行迭代并返回第一个
//相配的人。如果不起作用,则返回null
函数findImage(数组、模型){
for(过滤器变量中的var i){
var obj=过滤变量[i],
图像=对象图像;
for(图像中的var j){
if(比较(图像[j],模型))
返回图像[j];
}
}
返回null;
}
log(findImage(filteredVariants,{shot_type:“product”}))
const productImage = _.reduce(filteredVariants, (acc, item) => {
  return acc || _.find(item.images, image => image['shot_type'] === 'product')
}, undefined);