【问题标题】:Match array object with text of element将数组对象与元素文本匹配
【发布时间】:2021-12-28 14:21:43
【问题描述】:

我有一个对象数组,它们都遵循相同的格式和另一个对象数组

使用以下代码,我可以毫无问题地生成副标题。但是,我想要的是分隔生成的服务的子标题。小标题分隔基于services[k].category

这是我到目前为止所拥有的,但是它不起作用。副标题生成没有问题,但其余部分不起作用:

let categories = [{
    name: "Logo and Branding"
  },
  {
    name: "Web Design"
  },
  {
    name: "Print"
  },
  {
    name: "Presentations"
  },
  {
    name: "Art & Illustration"
  },
  {
    name: "Animation"
  }
]

let services = [{
  "name": "Logo",
  "description": "Capture the essence of your brand with an unforgettable logo design.",
  "icon": "logo.svg",
  "category": "logo"
}]

function generateServices(amount) {
  var content = "";

  for (let q = 0; q < categories.length; q++) {
    content += '<div class="col-12 main_titleCol"><div class="main_title"><h2 class="servicestitle">' + categories[q].name + '</h2></div></div>';
  }

  $('.services').html(content);

  let servicesheading = $('.servicestitle');
  // add the new items 
  for (let k = 0; k < amount; k++) {
    console.log(servicesheading.html);
    if (servicesheading.innerText == services[k].description) {
      content += '<div class="col-lg-4 col-md-4"><div class="feature_item"><img class="img-fluid services-icon" src="img/services/SVG/' + services[k].icon + '"><h4>' + services[k].name + '</h4><p>' + services[k].description + '</p></div></div>';
    }
  }
}

generateServices(10)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="services"></div>

如果一切正常,最终结果应该如下所示:

【问题讨论】:

  • 请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于 SO 的相关主题;如果您遇到困难,请发布您尝试的minimal reproducible example,注意输入和预期输出,最好在Stacksnippet
  • “我有一个对象数组......以及生成它们的以下代码” - 该“代码”创建一个标记字符串而不是单个对象 oO
  • “这是正确的方法吗?” - 试一试。如果它有效,答案将是“是”。如果没有minimal reproducible example,我们也许可以为您提供帮助。
  • @mplungjan 我更新了问题以更好地满足 StackOverflow 的要求,并展示了我的编码示例。希望这会更好。
  • @Andreas 试一试并展示了我的代码。希望它构成一个MVP

标签: javascript jquery arrays


【解决方案1】:

1.让我们按类别对我们的初始服务数组进行分组:

const services = [
  { name: '...', category: '...', 'description': '...', 'icon': '...' },
  { name: '...', category: '...', 'description': '...', 'icon': '...' },
  { name: '...', category: '...', 'description': '...', 'icon': '...' },
...
];
const servicesByCategory = services.reduce((acc,el) => {
  if (acc[el.category]) acc[el.category].push(el)
  else acc[el.category] = [el]; 
  return acc;
}, {});

servicesByCategory 是一个对象,其中键是服务类别,值是分配给类别的服务数组:

{
  service1: [ { name: '...', ... }, ... ],
  service2: [ { name: '...', ... }, ... ],
  service3: [ { name: '...', ... }, ... ],
  ...
}

2。现在您可以为每个类别一个一个地处理服务:

let content = '';
for (let category in servicesByCategory) {
  // Do what you want with category name
  // Let's add category header as an example 
  content += '<div class="col-12 main_titleCol"><div class="main_title"><h2 class="servicestitle">' + category + '</h2></div></div>';
  
  for (let service of servicesByCategory[category]) {
    // Proccess services one-by-one as you did before
    content += '<div class="col-lg-4 col-md-4"><div class="feature_item"><img class="img-fluid services-icon" src="img/services/SVG/' + service.icon + '"><h4>' + service.name + '</h4><p>' + service.description +'</p></div></div>';
  }
}

【讨论】:

  • 您能否解释一下第二个代码块中发生了什么。我无法理解这一点。我需要对我的类别数组做些什么来实现这一点?
  • @EGxo 它的servicesByCategory 结构示例。试用提供的代码,并在某处打印出servicesByCategory 值以了解我的意思。
  • 我玩弄了你的代码,试图弄清楚它,一切都很好。实际上,我是在自己弄清楚后才看到您的回复。这是一个了不起的解决方案,效果很好。感谢您在解释中如此透彻并帮助我解决了这个问题。很棒的工作+++
猜你喜欢
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多