更新-因为提供的帮助仍然没有返回所需的结果,并认为这可能是关于我如何提出的问题我是JSON的新手,正在努力在Javascript中处理这个结果。我使用这个fetch来获得一个响应,它作为一个带有嵌套对象的对象返回,我正在努力过滤只包含值“移动的”的类别的结果任何帮助将不胜感激!
v1l68za41#
如果你的响应是一个对象数组,其中每个对象都有一个变化的键和一个关联的对象,你可以使用Object.values来获取关联的对象,并检查第一个值的category。
Object.values
category
const response = [{ android8_gmailapp_pixel2_lm: { category: "web", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 8", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n', }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" }, }, { android8_gmailapp_pixel2_lm: { category: "mobile", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 10", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n', }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" } }]; const filteredResults = response.filter(obj => (([{ category }]) => category === 'mobile') (Object.values(obj))); console.log(filteredResults);
.as-console-wrapper { top: 0; max-height: 100% !important; }
dxxyhpgq2#
可以使用lodash pickBy:
pickBy
const results ={android8_gmailapp_pixel2_lm:{category:"web",client:"Gmail App",display_name:"Gmail App Pixel 2",id:"android8_gmailapp_pixel2_lm",os:"Android 8",screenshots:{default:"https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n"},status:"Complete",thumbnail:"https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n"},android8_gmailapp_pixel2_lm:{category:"mobile",client:"Gmail App",display_name:"Gmail App Pixel 2",id:"android8_gmailapp_pixel2_lm",os:"Android 10",screenshots:{default:"https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n"},status:"Complete",thumbnail:"https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n"}}; const filteredResults = _.pickBy(results, { category: 'mobile' }); console.log(filteredResults);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
h43kikqp3#
假设你的测试数据是一个对象数组(因为当前的形式不正确),
var testData = [ {android8_gmailapp_pixel2_lm: { category: "web", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 8", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n', }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" } }, { android8_gmailapp_pixel2_lm: { category: "mobile", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 10", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n' }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" } } ];
并且假设阵列中有更多设备,则以下操作应该可以正常工作:
let newList = []; testData.forEach(device => { let idx = Object.keys(device)[0]; if (device[idx].category === "mobile") { newList.push(device[idx]); }; });
然后,您应该会得到一个匹配项列表,如下所示:
[ { category: 'mobile', client: 'Gmail App', display_name: 'Gmail App Pixel 2', id: 'android8_gmailapp_pixel2_lm', os: 'Android 10', screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n' }, status: 'Complete', thumbnail: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n' } ]
// Online Javascript Editor for free // Write, Edit and Run your Javascript code using JS Online Compiler var a = [ {android8_gmailapp_pixel2_lm: { category: "web", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 8", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n', }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" } }, { android8_gmailapp_pixel2_lm: { category: "mobile", client: "Gmail App", display_name: "Gmail App Pixel 2", id: "android8_gmailapp_pixel2_lm", os: "Android 10", screenshots: { default: 'https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd…QUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG0ucG5n' }, status: "Complete", thumbnail: "https://images.emailonacid.com/d2ViL3BHeWNzYWpRZzd5Y1JoY3piUXJDbXBwdFpiWGhOOFN0bGROZkdLN1JQUzFkSy9hbmRyb2lkOF9nbWFpbGFwcF9waXhlbDJfbG1fdG4ucG5n" } } ] let newList = []; a.forEach(device => { let idx = Object.keys(device)[0]; if (device[idx].category === "mobile") { newList.push(device[idx]); }; }); console.log(newList);
3条答案
按热度按时间v1l68za41#
如果你的响应是一个对象数组,其中每个对象都有一个变化的键和一个关联的对象,你可以使用
Object.values
来获取关联的对象,并检查第一个值的category
。dxxyhpgq2#
可以使用lodash
pickBy
:h43kikqp3#
假设你的测试数据是一个对象数组(因为当前的形式不正确),
并且假设阵列中有更多设备,则以下操作应该可以正常工作:
然后,您应该会得到一个匹配项列表,如下所示: